Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.


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.

Managed types in C++/CLI are supported by garbage collection. Where native C++ types require using new and delete, managed C++ C++/CLI types need only gcnew.

// 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 {
   void Test() {}

   value class Value_Nested_Class {
      int i;

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

Compiler option: /clr

Community Additions

© 2015 Microsoft