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

﹥>﹥吖頭↗ 提交于 2019-11-25 21:57:31

问题


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


回答1:


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




回答2:


// 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^.




回答3:


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.




回答4:


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




回答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




回答6:


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




回答7:


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



来源:https://stackoverflow.com/questions/202463/what-does-the-caret-mean-in-c-cli

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!