A.3 Microsoft Visual C++

Microsoft's latest C++ compiler has a split personality. It can generate a conventional program following the standard (more or less), or it can produce .NET output using a modified language called Managed C++, which restricts some features and adds others. The following are some highlights of the Managed C++ extensions:

_ _box

The _ _box operator takes a value object as an argument and returns a managed (_ _gc) object that wraps the value in a managed "box." You can also declare a pointer to a value type with the _ _box specifier so the pointer can store boxed values. The compiler treats boxed values specially and lets you access the members of the value transparently, as though the box were not present. Nonetheless, the box manages the lifetime of the value it contains.

_ _declspec

The _ _declspec keyword takes a list of attributes in parentheses and serves as a declaration specifier. Depending on the attributes, it can be used to modify a function, object, or class. See Section A.1 for more information.

_ _gc

The key feature of Managed C++ is that objects are garbage-collected. This means the lifetime and memory are managed automatically by the runtime environment. As long as the object is in use, it remains alive. When the object is no longer used anywhere in the program, it is up for reclamation.

Declare a class using the _ _gc specifier to mark the class as managed. A managed class has numerous restrictions, such as:

  • No more than one base class (which must also be managed)

  • No unmanaged data members (except POD types)

  • No friends

  • No operator new or operator delete members

  • No user-defined copy constructors

  • No const or volatile qualified member functions

Managed objects are created with the global new operator. They are freed automatically when they are no longer needed. If the class has a destructor, you can invoke the delete operator, which calls the destructor but does not free the memory.

_ _int64

The _ _int64 type is a 64-bit integer type. In the current releases of Visual C++, long is 32 bits. A 64-bit integer literal is written with a suffix of i64 (e.g., 10000000000000i64).

_ _pin

Sometimes, a managed application must call an unmanaged library or system call. The _ _pin keyword locks a managed object at its address in memory and prevents the garbage collector from moving the object. The address of the object can safely be passed to the unmanaged function. When the _ _pin pointer goes out of scope, the managed object is no longer pinned and can be moved or reclaimed.

_ _property

A property is a pseudo-member that is used like a data member, but it can have the semantics of a member function. Properties are the foundation of the Visual Studio RAD features. A property is associated with a getter and setter, which have the forms get_name( ) and set_name( ) for the property named name:

_  _gc class Control {


  int height_;


  _  _property int get_height(  ) const { return height_; }

  _  _property void set_height(int h);


Control * ctl = new Control;

ctl->height = 10;    // Calls ctl->set_height(10)

int h = ctl->height; // Calls ctl->get_height(  )
_ _value

A _ _value class is intended for small objects with short lifetimes, which are allocated on the runtime stack, not the managed heap (the way _ _gc objects are managed). A managed class can have _ _value data members, but not the other way around.


The #using directive is similar to #include, except the included object is not a header or file but a .NET assembly, which contains all the information the compiler needs to use the classes, types, and functions that are defined in the assembly.