.net - 为什么在以下有关对象类的代码中,在 Point 后面使用 ^ 运算符

标签 .net object c++-cli msdn

这是来自 MSDN 的有关 .NET Framework 中对象类的示例。

using namespace System;

// The Point class is derived from System.Object.
ref class Point
{
public:
    int x;
public:
    int y;

public:
    Point(int x, int y)
    {
        this->x = x;
        this->y = y;
    }

public:
    virtual bool Equals(Object^ obj) override
    {
        // If this and obj do not refer to the same type,
        // then they are not equal.
        if (obj->GetType() != this->GetType())
        {
            return false;
        }

        // Return true if  x and y fields match.
        Point^ other = (Point^) obj;
        return (this->x == other->x) && (this->y == other->y);
    }

    // Return the XOR of the x and y fields.
public:
    virtual int GetHashCode() override 
    {
        return x ^ y;
    }

    // Return the point's value as a string.
public:
    virtual String^ ToString() override 
    {
        return String::Format("({0}, {1})", x, y);
    }

    // Return a copy of this point object by making a simple
    // field copy.
public:
    Point^ Copy()
    {
        return (Point^) this->MemberwiseClone();
    }
};

int main()
{
    // Construct a Point object.
    Point^ p1 = gcnew Point(1, 2);

    // Make another Point object that is a copy of the first.
    Point^ p2 = p1->Copy();

    // Make another variable that references the first
    // Point object.
    Point^ p3 = p1;

    // The line below displays false because p1 and 
    // p2 refer to two different objects.
    Console::WriteLine(
        Object::ReferenceEquals(p1, p2));

    // The line below displays true because p1 and p2 refer
    // to two different objects that have the same value.
    Console::WriteLine(Object::Equals(p1, p2));

    // The line below displays true because p1 and 
    // p3 refer to one object.
    Console::WriteLine(Object::ReferenceEquals(p1, p3));

    // The line below displays: p1's value is: (1, 2)
    Console::WriteLine("p1's value is: {0}", p1->ToString());
}

// This code produces the following output.
//
// False
// True
// True
// p1's value is: (1, 2)

我不明白的是为什么在Point之后使用^运算符。 哪位好心人给解释一下。

最佳答案

这是 C++/CLI,而不仅仅是普通的 C++。

^ 基本上是普通 * 的 .NET 等价物,但它定义了一个句柄,而不是一个指针。句柄可以为空,就像指针一样。

因此,Point^Point 类实例的句柄。

您可以read more about it on MSDN .

关于.net - 为什么在以下有关对象类的代码中,在 Point 后面使用 ^ 运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9390167/

相关文章:

java - 从类创建对象数组

.net - 如何在Python中加密并在C#中解密?

java - 如何在 Java 中按字母顺序组织对象数组

c# - 如何以一种形式发布两个或多个模型?

java - 如何检查对象数组中对象的重复值,合并重复值,然后删除重复值?

c# - 如何使用 C++/CLI Wrapper 将变量参数从托管传递到非托管?

.net - C++/cli 识别托管/非托管之间的转换并测量其成本

c++ - 将 C++/CLI 字符串数组转换为 native C++ 字符**

.net - 如何将 ASP.NET MVC 应用程序中的模型、 View 和 Controller 分成不同的程序集

c# - 我如何启动进程并隐藏它的窗口?