【发布时间】:2012-10-30 13:22:14
【问题描述】:
好吧,WinAPI 有一个POINT 结构,但我正在尝试创建一个替代类,以便您可以从构造函数中设置x 和y 的值。这很难用一句话来解释。
/**
* 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;
是否可以以某种方式重载operator=,以便我可以将Point 分配给POINT?或者可能有其他方法来实现这一点?
【问题讨论】:
标签: c++ oop casting operator-overloading assignment-operator