什么是“^”? C++ 中的符号?
当我在岩石下睡觉时,是否有新符号加入了 C++ 语言规范?
我刚刚遇到以下问题:
这表明 '^' 符号在某种程度上是 C++ 的一部分(不是按位异或的传统含义)
是这样吗?
如果是这样,这意味着什么? (我尝试google 问题,但 Google 没有给出令人满意的结果答案)
Has a new symbol joined the C++ language specification while I was sleeping under a rock?
I just encountered the following question:
Restrict Text Box to only accept 10 digit number
Which suggests that the '^' symbol is somehow part of C++ (not in the legacy meaning of a bitwise-XOR)
Is this so?
If so, what does it mean? (I tried to google the question but Google didn't come up with satisfactory answers)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 Visual C++ 中,
^
表示托管对象的句柄。本质上,C# 中的内容是参考。使用gcnew
而不是new
来分配它们,它们将会被垃圾回收。这就是 Visual C++ 与 CLI 交互的方式。In Visual C++,
^
represents a handle to a managed object. Essentially what in C# would be a reference. Allocate them withgcnew
instead ofnew
, and they will be garbage collected for you. This is how Visual C++ interacts with the CLI.在引用的答案中,它不是标准 C++ 语言的一部分,而是 Microsoft 为 .NET 互操作而拼凑在一起的 C++/CLI 语言的一部分。在该语言中,^ 表示“指向托管内存的指针”。
In the referenced answer, it's not part of the standard C++ language, it's part of the C++/CLI language that Microsoft cobbled together for .NET interop. In that language, ^ means a "pointer to managed memory."
“^”语法引用跟踪参考 在 C++/CLI 中,Microsoft 对 C++ 的扩展,支持交互与托管代码。
The '^' syntax refers to a tracking reference in C++/CLI, a Microsoft extension to C++ which enables interaction with managed code.
它不是标准 C++ 的一部分。它是托管 C++(Microsoft 的语言,很像 .NET 的 C++)的一部分。它的意思是“对 ---- 的引用”,就像标准 C++ 中“*”表示“指向 ---- 的指针”一样。
It's not part of Standard C++. It's part of Managed C++ (Microsoft's language much like C++ for .NET). It means "a reference to ----" in much the same way a "*" means "A pointer to -----" is Standard C++.
在 C++ 中,“^”符号是按位异或 (xor) 运算符。对于单个位,您有
0 ^ 0 = 1 ^ 1 = 0
和0 ^ 1 = 1 ^ 0 = 1
。但是,在您提到的问题中,它是 Microsoft 在 .NET 平台上进行 C++ 开发的特殊语法的一部分,称为 C++/CLI 或 It Just Works。
.NET 上的内存被垃圾收集,并且必须跟踪对对象的引用。这使得无法使用普通的 C++ 指针来引用这些对象。微软决定重用“^”符号来声明一个有点类似于指针的变量,可以引用托管堆上的对象。
^(托管堆上的对象句柄)
In C++ the “^” symbol is the bitwise exclusive or (xor) operator. For a single bit you have
0 ^ 0 = 1 ^ 1 = 0
and0 ^ 1 = 1 ^ 0 = 1
.However, in the question you are refering to it is part of Microsoft special syntax for C++ development on the .NET platform known as C++/CLI or It Just Works.
Memory on .NET is garbage collected and references to objects will have to be tracked. This makes it impossible to reference these objects using a normal C++ pointer. Microsoft has decided to reuse the “^” symbol to declare a variable somewhat similar to a pointer that can reference an object on the managed heap.
^ (Handle to Object on Managed Heap)