C++ OOP - Can you 'overload a cast' <- hard to explain in 1 sentence

Posted by Brandon Miller on Stack Overflow See other posts from Stack Overflow or by Brandon Miller
Published on 2012-11-10T10:48:43Z Indexed on 2012/11/10 11:00 UTC
Read the original article Hit count: 201

Well, the WinAPI has a POINT struct, but I am trying to make an alternative class to this so you can set the values of x and y from a constructor.

/**
 * X-Y coordinates
 */
class Point {
  public:
    int X, Y;

    Point(void)            : X(0),    Y(0)    {}
    Point(int x, int y)    : X(x),    Y(y)    {}
    Point(const POINT& pt) : X(pt.x), Y(pt.y) {}

    Point& operator= (const POINT& other) {
        X = other.x;
        Y = other.y;
    }
};

// I have an assignment operator and copy constructor.
Point myPtA(3,7);
Point myPtB(8,5);

POINT pt;
pt.x = 9;
pt.y = 2;

// I can assign a 'POINT' to a 'Point'
myPtA = pt;

// But I also want to be able to assign a 'Point' to a 'POINT'
pt = myPtB;

Is it possible to overload operator= in a way so that I can assign a Point to a POINT? Or maybe some other method to achieve this?

Thanks in advance.

© Stack Overflow or respective owner

Related posts about c++

Related posts about oop