Skip to content

Latest commit

 

History

History
110 lines (93 loc) · 2.97 KB

ref-new-gcnew-cpp-component-extensions.md

File metadata and controls

110 lines (93 loc) · 2.97 KB
title ref new, gcnew (C++ Component Extensions) | Microsoft Docs
ms.custom
ms.date 11/04/2016
ms.reviewer
ms.suite
ms.technology
devlang-cpp
ms.tgt_pltfrm
ms.topic language-reference
f1_keywords
gcnew
ref new
gcnew_cpp
dev_langs
C++
helpviewer_keywords
ref new keyword (C++)
gcnew keyword [C++]
ms.assetid 388a62da-c2df-4a94-a9a2-205b53e577da
caps.latest.revision 24
author mikeblome
ms.author mblome
manager ghogen
translation.priority.ht
cs-cz
de-de
es-es
fr-fr
it-it
ja-jp
ko-kr
pl-pl
pt-br
ru-ru
tr-tr
zh-cn
zh-tw

ref new, gcnew (C++ Component Extensions)

The ref new aggregate keyword allocates an instance of a type that is garbage collected when the object becomes inaccessible, and that returns a handle (^) to the allocated object.

All Runtimes

Memory for an instance of a type that is allocated by ref new is deallocated automatically.

A ref new operation throws OutOfMemoryException if it is unable to allocate memory.

For more information about how memory for native C++ types is allocated and deallocated, see the new and delete operators.

[!INCLUDEwrt]

Use ref new to allocate memory for Windows Runtime objects whose lifetime you want to administer automatically. The object is automatically deallocated when its reference count goes to zero, which occurs after the last copy of the reference has gone out of scope. For more information, see Ref classes and structs.

Requirements

Compiler option: /ZW

Memory for a managed type (reference or value type) is allocated by gcnew, and deallocated by using garbage collection.

Requirements

Compiler option: /clr

Examples

Example

The following example uses gcnew to allocate a Message object.

// mcppv2_gcnew_1.cpp  
// compile with: /clr  
ref struct Message {  
   System::String^ sender;  
   System::String^ receiver;  
   System::String^ data;  
};  
  
int main() {  
   Message^ h_Message  = gcnew Message;  
  //...  
}  

Example

The following example uses gcnew to create a boxed value type for use like a reference type.

// example2.cpp : main project file.  
// compile with /clr  
using namespace System;  
value class Boxed {  
    public:  
        int i;  
};  
int main()  
{  
    Boxed^ y = gcnew Boxed;  
    y->i = 32;  
    Console::WriteLine(y->i);  
    return 0;  
}  

Output

32  

See Also

Component Extensions for Runtime Platforms