Managed C++ Extensions
The common language runtime defines two types of managed elements: managed code and managed data. Managed code cooperates closely with the common language runtime. This means simply that managed code provides the necessary metadata so the runtime can provide its services. Remember that the runtime provides memory management services, cross-language integration services, code access security services, and automatic lifetime control for objects. The runtime needs to know everything about the code it's hosting so it can provide these services.The common language runtime also manages your application's data; that is, the runtime manages object layout. It also manages object references within your applications, releasing them when they're no longer being used. These objects are known as managed data.So how do you write managed code? A handful of new keywords, applied judiciously, get rid of all the headaches associated with tracking pointers and memory and with mismatching types accidentally.Writing .NET code using C++ turns out to be fairly straightforward. All it takes is a few new keywords and symbols placed in the correct place. Table 32-1 describes Managed Extensions for C++.
Extension Keyword | Functionality |
---|---|
__abstract | Types declared as __abstract cannot be instantiated directly. |
__box | __value classes that apply the __box extension have a copy created on the common language runtime heap. |
__delegate | Types declared as __delegate reference a unique method of a managed class (like a function pointer). |
__event | Types declared using __event define an event method of a managed class. |
__finally | Code within a __finally block becomes associated with the previous try block. |
__gc | Types declared using __gc live on the managed heap. |
__identifier | Tokens that apply the __identifier extension allow C++ keywords to be used as identifiers. |
__interface | Types that apply the __interface keyword are declared as managed interfaces. |
__nogc | Native C++ classes that apply the __nogc extension are not garbage-collected. |
__pin | Objects that apply the __pin extension are prevented from being moved by the common language runtime during garbage collection. |
__property | Fields that use the __property extension declare a property member for a managed class. |
public, protected, and private | Types that apply these extensions define their visibility outside of an assembly. Fields (member variables and member functions) that use these extensions define their visibility within an assembly. |
__sealed | __gc classes that use the __sealed extension cannot be used as a base class. This extension also prevents methods from being overridden in a derived class. |
__try_cast | Using the __try_cast extension attempts the specified cast. The cast throws an exception on failure. |
__typeof | This extension gets the System::Type of an instance of a type. |
__value | Types that apply the __value extension are of the value type. |