254

I just came across this code and a few Google searches turn up no explanation of this mysterious (to me) syntax.

Hashtable^ tempHash = gcnew Hashtable(iterators_);

IDictionaryEnumerator^ enumerator = tempHash->GetEnumerator();

What the heck does the caret mean? (The gcnew is also new to me, and I asked about that here.)

Community
  • 1
  • 1
Owen
  • 7,494
  • 10
  • 42
  • 52
  • 9
    By the way, it is [pronounced "hat"](https://msdn.microsoft.com/en-us/library/yk97tc08(v=vs.120).aspx). – kmote Oct 21 '15 at 14:34

8 Answers8

205

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.)

ProfNandaa
  • 3,210
  • 2
  • 18
  • 19
Rob Walker
  • 46,588
  • 15
  • 99
  • 136
106
// 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^.

Bakudan
  • 19,134
  • 9
  • 53
  • 73
salomon
  • 1,219
  • 1
  • 8
  • 9
  • 2
    I would rather say why not ^mngd instead of * mngd.. It's confusing all of a sudden allowed to use unmanaged pointer symbol(*), not the managed pointer(^) in front of actually variable declared in managed heap. – swcraft Jul 22 '16 at 15:08
  • Thank you! I was looking for an explanation for the `%` punctuator. – Stefan Nov 30 '21 at 16:37
26

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.

Franci Penov
  • 74,861
  • 18
  • 132
  • 169
  • The caret (handle) is more like a pointer to a managed object than a reference to one. See https://web.archive.org/web/20150117095313/http://msdn.microsoft.com/en-us/library/yk97tc08(VS.80).aspx and/or https://learn.microsoft.com/en-us/cpp/extensions/tracking-reference-operator-cpp-component-extensions?view=msvc-160 – AJM Mar 02 '21 at 16:47
  • Um, sorry, https://learn.microsoft.com/en-us/cpp/extensions/handle-to-object-operator-hat-cpp-component-extensions?view=msvc-160 is more useful than the second link in my last comment. – AJM Mar 02 '21 at 16:48
23

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.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
5

In C++/CLI it means a managed pointer. You can read more about it (and other C++/CLI features) here:

http://en.wikipedia.org/wiki/C%2B%2B/CLI

Mark Ingram
  • 71,849
  • 51
  • 176
  • 230
5

From MSDN, it looks like the caret means you are getting a handle to the type being created.

https://web.archive.org/web/20150117095313/http://msdn.microsoft.com/en-us/library/te3ecsc8%28VS.80%29.aspx

Community
  • 1
  • 1
Jon Tackabury
  • 47,710
  • 52
  • 130
  • 168
2

It means that it is a reference to a managed object.

1800 INFORMATION
  • 131,367
  • 29
  • 160
  • 239
0

It's also worth considering the following couple of sentences, that put the answer in a slightly different way:

"The handle declarator (^, pronounced "hat"), modifies the type specifier to mean that the declared object should be automatically deleted when the system determines that the object is no longer accessible."

"Because native C++ pointers (*) and references (&) are not managed references, the garbage collector cannot automatically update the addresses they point to. To solve this problem, use the handle declarator to specify a variable that the garbage collector is aware of and can update automatically."

(And "native" is I.M.H.O. a better word than 'handle', as handle is possibly a word that was brought more so in by the use of the 'Windows SDK')

DennisVM-D2i
  • 416
  • 3
  • 8