.net C++中的“^”符号是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3621847/
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 14:41:04  来源:igfitidea点击:

What is the "^" symbol in C++?

.netvisual-c++c++-cli

提问by Android Eve

Has a new symbol joined the C++ language specification while I was sleeping under a rock?

当我在一块石头下睡觉时,是否有一个新符号加入了 C++ 语言规范?

I just encountered the following question:

我刚刚遇到以下问题:

Restrict Text Box to only accept 10 digit number

限制文本框只接受 10 位数字

Which suggests that the '^' symbol is somehow part of C++ (not in the legacy meaning of a bitwise-XOR)

这表明“^”符号在某种程度上是 C++ 的一部分(不是按位异或的传统含义)

Is this so?

是这样吗?

If so, what does it mean? (I tried to google the questionbut Google didn't come up with satisfactory answers)

如果是这样,这意味着什么?(我试图用谷歌搜索这个问题,但谷歌没有给出满意的答案)

回答by Martin Liversage

In C++ the “^” symbol is the bitwise exclusive or (xor) operator. For a single bit you have 0 ^ 0 = 1 ^ 1 = 0and 0 ^ 1 = 1 ^ 0 = 1.

在 C++ 中,“^”符号是按位异或 (xor) 运算符。对于一点,你有0 ^ 0 = 1 ^ 1 = 00 ^ 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.

但是,在您所指的问题中,它是 Microsoft 在 .NET 平台上进行 C++ 开发的特殊语法的一部分,称为 C++/CLI 或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.

.NET 上的内存被垃圾收集,并且必须跟踪对对象的引用。这使得无法使用普通的 C++ 指针来引用这些对象。Microsoft 已决定重用“^”符号来声明一个变量,该变量有点类似于可以引用托管堆上的对象的指针。

^ (Handle to Object on Managed Heap)

^(托管堆上的对象句柄)

回答by Nick Lewis

In Visual C++, ^represents a handle to a managed object. Essentially what in C# would be a reference. Allocate them with gcnewinstead of new, and they will be garbage collected for you. This is how Visual C++ interacts with the CLI.

在 Visual C++ 中,^表示托管对象的句柄。本质上,C# 中的内容将是一个参考。使用gcnew而不是分配它们new,它们将为您进行垃圾收集。这就是 Visual C++ 与 CLI 交互的方式。

回答by Joel

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++ 语言的一部分,而是 Microsoft 为 .NET 互操作拼凑而成的 C++/CLI 语言的一部分。在该语言中,^ 表示“指向托管内存的指针”。

回答by Stu Mackellar

The '^' syntax refers to a tracking referencein C++/CLI, a Microsoft extension to C++ which enables interaction with managed code.

的“^”的语法是指跟踪参考C ++ / CLI,Microsoft扩展到C ++,其使得能够与托管代码的相互作用。

回答by James Curran

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++ 的一部分。它是托管 C++(微软的语言,很像 .NET 的 C++)的一部分。它的意思是“对 ---- 的引用”,与标准 C++ 中的“*”的意思是“指向 ----- 的指针”大致相同。