插入符号 (‘^’) 在 C++/CLI 中意味着什么?

发布于 2024-07-07 10:33:15 字数 332 浏览 12 评论 0 原文

我刚刚发现了这段代码,谷歌搜索了一些,但没有找到对这个神秘的(对我来说)语法的解释。

Hashtable^ tempHash = gcnew Hashtable(iterators_);

IDictionaryEnumerator^ enumerator = tempHash->GetEnumerator();

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

你是年少的欢喜 2024-07-14 10:33:15

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

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

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

蓝海 2024-07-14 10:33:15
// 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

一般来说,标点符号 % 对应于 ^,就像标点符号 & 对应于 * 一样。 在 C++ 中,一元 & 运算符在 C++/CLI 中为一元 % 运算符。

&ptr 产生 P*,而 %mngd 产生 MO^

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

圈圈圆圆圈圈 2024-07-14 10:33:15

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

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.

蓝海似她心 2024-07-14 10:33:15

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

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.

—━☆沉默づ 2024-07-14 10:33:15

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

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

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

夢归不見 2024-07-14 10:33:15

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

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

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

何止钟意 2024-07-14 10:33:15

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

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

惯饮孤独 2024-07-14 10:33:15

还值得考虑以下几句话,它们以稍微不同的方式给出答案:

“句柄声明符(^,称为“插入符”或“帽子”或“circumflex”)修改类型说明符以表示当系统确定该对象不再可访问时,应自动删除所声明的对象。”

“由于本机 C++ 指针 (*) 和引用 (&) 不是托管引用,因此垃圾收集器无法自动更新它们指向的地址。要解决此问题,请使用句柄声明符指定垃圾收集器识别的变量并且可以自动更新。”

(在我看来,“本机”是比“句柄”更好的词,因为“句柄”可能是通过使用“Windows SDK”而引入的词)

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

"The handle declarator (^, referred to as "caret" or "hat" or "circumflex"), 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 in my humble opinion a better word than 'handle', as handle is possibly a word that was brought more so in by the use of the 'Windows SDK')

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文