.net C++/CLI 中的插入符号 ('^') 是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/202463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 10:20:15  来源:igfitidea点击:

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

.netc++-cli

提问by Owen

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 gcnewis also new to me, and I asked about that here.)

插入符号到底是什么意思?(这gcnew对我来说也是新的,我在这里问过这个。)

采纳答案by Rob Walker

This is C++/CLIand 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).

这是C++/CLI,插入符号是 *(指针)的托管等价物,在 C++/CLI 术语中,它被称为“引用类型”的“句柄”(因为您仍然可以拥有非托管指针)。

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

(感谢 Aardvark 指出更好的术语。)

回答by salomon

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

通常,标点符号%to^就像标点符号&to 一样*。在 C++ 中,一元运算&符在 C++/CLI 中是一元运算%符。

While &ptryields a P*, %mngdyields at MO^.

虽然&ptr产生 a P*,但%mngd产生于MO^

回答by Franci Penov

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.

这意味着这是对托管对象与常规 C++ 指针的引用。此类引用背后的对象由运行时管理并可在内存中重新定位。它们也会自动进行垃圾收集。

回答by Joel Coehoorn

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 correctplace even if the garbage collector moves the object it points to.

当您分配托管内存时,垃圾收集器可以移动该内存。^ 运算符是托管内存的指针,即使垃圾收集器移动了它指向的对象,它也会继续指向正确的位置。

回答by Jon Tackabury

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

从 MSDN,它看起来像插入符号意味着您正在获取正在创建的类型的句柄。

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

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

回答by Mark Ingram

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

在 C++/CLI 中,它表示托管指针。您可以在此处阅读有关它(以及其他 C++/CLI 功能)的更多信息:

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

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

回答by 1800 INFORMATION

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

这意味着它是对托管对象的引用。