3

This is an Example from MSDN about Object Class in .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)

What i dont understand is why is the ^ operator used after Point. Someone Kindly Explain.

0

3 Answers 3

3

This is C++/CLI, not just plain C++.

The ^ is basically the .NET equivalent of ordinary *, but it defines a handle, rather than a pointer. Handles can be null, just like pointers.

So Point^ is a handle to an instance of the Point class.

You can read more about it on MSDN.

1

The ^ operator is the managed C++ way of creating handles. See here

1

This is C++/CLI, formerly known as Managed C++ code, where you can mix standard C++ and use .Net libraries as well.

The ^ hat operator declares the variable to be a Garbage Collected Handle as opposed to a standard C style pointer * or a C++ reference &.

2
  • so with ^ operator c++ collects garbage automatically like Java? Feb 22, 2012 at 7:04
  • 1
    Yes, although there are limitations to it. Objects cannot have destructors, they must only have primitive types and all other objects must be stored with handles themselves. Feb 22, 2012 at 11:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.