Managed Extensions for C++ (also called Managed C++, or MC++) is the first way Microsoft let C++ write .NET code. It has the same .cpp and .h files and the same #using lines as C++/CLI, and different words: the keywords carry two leading underscores, and managed objects are held by ordinary pointers. The viewer shows such a file as “Managed Extensions for C++ source file”, with the same colours as C++/CLI. Versions and history says when each was current.
The keywords and what replaced them
| Managed Extensions | Meaning | C++/CLI |
|---|---|---|
__gc class, __gc struct | a reference type | ref class, ref struct |
__value class, __value struct | a value type | value class, value struct |
__value enum, __gc enum | a managed enumeration | enum class |
__gc __interface | a managed interface | interface class |
__nogc | a native class, where a class would otherwise be managed | class, struct (native by default) |
__abstract, __sealed | an abstract class, a class that cannot be derived from | abstract, sealed |
__property with get_X and set_X functions | a property | property T X { get; set } |
__delegate | a delegate type | delegate |
__event | an event | event |
__box(x) | box a value | implicit boxing, or Object^ o = x; |
__pin | a pinning pointer | pin_ptr<T> |
__try_cast<T*>(x) | a cast that throws when it fails | safe_cast<T^>(x) |
__typeof(T) | the System::Type of a type | T::typeid |
__identifier(x) | a keyword used as a name | the same |
Foo* (a managed pointer) | a reference to a managed object | Foo^ |
new Foo(args) (on a __gc class) | allocation on the managed heap | gcnew Foo(args) |
Int32 a[], new Int32[5] | a managed array | array<Int32>^, gcnew array<Int32>(5) |
S"text" | a managed string literal | "text" (converts to String^) |
#using <mscorlib.dll>
using namespace System;
__gc class Counter {
int n;
public:
__property int get_Count() { return n; }
__property void set_Count(int v) { n = v; }
void Add() { n++; }
};
__value struct Point { int x; int y; };
__delegate void Notify(Object* sender, EventArgs* e);
int main() {
Counter* c = new Counter();
c->Add();
Console::WriteLine(c->Count);
Object* boxed = __box(5);
return 0;
}
A __gc class could have only one base class and no native members by value, as a ref class still cannot; its objects were created with new and held in * pointers that the compiler tracked. The double-underscore words are reserved in every context in this syntax, which is why the viewer colours them as keywords everywhere.
Not described
Known, not yet covered
- Reference: the Managed Extensions pages of the Visual Studio .NET 2002 and 2003 documentation were not retrieved: this table is written from knowledge of the language, not checked against its specification, and the details of pointers, arrays and attributes in that syntax are not described.
- Viewer: the managed string literal
S"text"is read as an identifierSfollowed by a string, so theSis left plain.