C++/CLI has two worlds of type in one file. A native type is declared with class, struct or union, and is an ordinary C++ type. A managed type is declared with one of the spaced keywords below, is described by metadata in the assembly, and is a .NET type that C#, Visual Basic and F# can use.
The three new kinds of type
| Declaration | .NET type | Lives | Default access |
|---|---|---|---|
ref class Name | a class (derives from System::Object unless a base is named) | managed heap, held by a handle Name^ | private |
ref struct Name | a class | managed heap | public |
value class Name | a structure (derives from System::ValueType) | stack, or inside another object; copied on assignment | private |
value struct Name | a structure | as above | public |
interface class Name | an interface | no objects of its own | public; every member is abstract |
interface struct Name | an interface | no objects of its own | public |
enum class Name, enum struct Name | an enumeration | value | public |
delegate R Name(…) | a delegate (derives from System::MulticastDelegate) | managed heap | – |
public interface class IShape {
property double Area { double get(); }
void Draw();
};
public value struct Point { int X; int Y; };
public enum class Colour : int { Red, Green = 5, Blue };
public ref class Circle sealed : IShape {
double radius;
public:
Circle(double r) : radius(r) {}
property double Area { virtual double get() { return Math::PI * radius * radius; } }
virtual void Draw() { Console::WriteLine("circle {0}", Area); }
};
Reference types
- A ref class has one base class (a ref class;
System::Objectwhen none is named) and any number of interfaces. Multiple inheritance of classes is not allowed. - An object is created with
gcnewand held in a handle (Handles and references). A local variable written without a handle (Circle c(2.0);) has stack semantics: the compiler creates the object on the heap, and calls the destructor, which isDispose, at the end of the scope. abstractafter the name makes the class abstract,sealedstops it being a base. Both are context-sensitive keywords (Keywords).- A ref class may have a static constructor (
static Name() { … }), which runs once before the first use of the type, a destructor~Name()that isIDisposable.Dispose, and a finalizer!Name(). - A ref class cannot contain a native class by value, only a pointer to one, and cannot be a member of a native class by value, only through
gcroot<T^>(Interop). It cannot have friends.
Value types
- A value class is copied on assignment and on a call. It derives from
System::ValueType, cannot be a base class, and can implement interfaces. - It has no user-written default constructor and no destructor or finalizer.
- A handle to a value type is a boxed value:
int^ b = 5;. Boxing is implicit when a value is assigned toObject^, andsafe_castor the dereference*bunboxes it.
Interfaces and enumerations
- An interface class lists members without bodies: functions, properties, events and static members. A ref class implements it with
: IShapeand may name the interface function it implements (Members). - An enum class has an underlying type (
intunless: Byteor another integral type follows the name). Its enumerators are scoped:Colour::Red. There is no implicit conversion to an integer. An attribute[Flags]marks a set of bit flags.
Visibility
A managed type that is not declared public is visible only inside its assembly, as a C# internal type is. A public ref class is part of the interface that other languages see. The access labels of members are public, private, protected, internal (assembly), and the combinations public protected, protected private and public private. A native type that a public type uses in its signature must be made public with #pragma make_public (Mixed mode).
Arrays and strings
array<int>^ a = gcnew array<int>(10);
array<int>^ b = gcnew array<int>{ 1, 2, 3 };
array<int, 2>^ grid = gcnew array<int, 2>(3, 4);
array<array<int>^>^ jagged = gcnew array<array<int>^>(5);
int n = a->Length;
String^ s = "text";
String^ t = s + " more" + 42;
A managed array is a reference type (System::Array), always held by a handle. Its rank is the second template argument. A string literal converts to String^ implicitly; + concatenates, and the types convert with ToString. A System::String is immutable and holds UTF-16.
How C++ types map to .NET
| C++ | .NET type |
|---|---|
bool | System::Boolean |
char (plain or signed) | System::SByte |
unsigned char | System::Byte |
wchar_t | System::Char |
short, unsigned short | System::Int16, System::UInt16 |
int, long, and their unsigned forms | System::Int32, System::UInt32 |
long long, __int64, and the unsigned forms | System::Int64, System::UInt64 |
float, double | System::Single, System::Double |
void | System::Void |
Not described
Known, not yet covered
- Reference: the full list of restrictions on managed types (what a ref class may not contain or declare, the rules for copy constructors and assignment, and the layout attributes of value types) is given in part only. The type-mapping table is written from knowledge of the language and was not compared with the Microsoft page row by row.