Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the caret (‘^’) mean in C++/CLI?

Tags:

.net

c++-cli

People also ask

What does caret mean in C?

Arithmetic Operators Note: Origin C, by default, treats the caret character (^) as an exponentiate operator . This is done to be consistent with LabTalk. ANSI C uses the caret character as the exclusive OR operator.

What is Gcnew in Visual C++?

gcnew is an operator, just like the new operator, except you don't need to delete anything created with it; it's garbage collected. You use gcnew for creating . Net managed types, and new for creating unmanaged types.


This is C++/CLI and the caret is the managed equivalent of a * (pointer) which in C++/CLI terminology is called a 'handle' to a 'reference type' (since you can still have unmanaged pointers).

(Thanks to Aardvark for pointing out the better terminology.)


// here normal pointer
P* ptr = new P; // usual pointer allocated on heap
P& nat = *ptr; // object on heap bind to native object

//.. here CLI managed 
MO^ mngd = gcnew MO; // allocate on CLI heap
MO% rr = *mngd; // object on CLI heap reference to gc-lvalue

In general, the punctuator % is to ^ as the punctuator & is to *. In C++ the unary & operator is in C++/CLI the unary % operator.

While &ptr yields a P*, %mngd yields at MO^.


It means that this is a reference to a managed object vs. a regular C++ pointer. Objects behind such references are managed by the runtime and can be relocated in the memory. They are also garbage-collected automatically.


When you allocated managed memory, that memory can be moved around by the garbage collector. The ^ operator is a pointer for managed memory which continues to point to the correct place even if the garbage collector moves the object it points to.