Export (0) Print
Expand All
19 out of 28 rated this helpful - Rate this topic

gcnew

gcnew creates an instance of a managed type (reference or value type) on the garbage collected heap. The result of the evaluation of a gcnew expression is a handle (^) to the type being created.

// mcppv2_gcnew_1.cpp
// compile with: /clr
ref struct Message {
   System::String ^ sender, ^ receiver, ^ data;
};

int main() {
   Message ^ h_Message  = gcnew Message ;
}

It is possible to create an instance of a managed type, where the managed type contains a nested type other than a reference type:

// mcppv2_gcnew_2.cpp
// compile with: /clr
ref class MyClass {
public:
   void Test() {}

   value class Value_Nested_Class {
   public:
      int i;
   };
};

int main() {
   MyClass ^ h_MyClass = gcnew MyClass;
   
   MyClass::Value_Nested_Class y;
   y.i = 32;
   System::Console::WriteLine(y.i);
}

Output

32

Compiler option: /clr

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.