【发布时间】:2013-02-22 02:15:09
【问题描述】:
考虑:
class MyObject{
public:
MyObject();
MyObject(int,int);
int x;
int y;
MyObject operator =(MyObject rhs);
};
MyObject::MyObject(int xp, int yp){
x = xp;
y = yp;
}
MyObject MyObject::operator =(MyObject rhs){
MyObject temp;
temp.x = rhs.x;
temp.y = rhs.y;
return temp;
}
int main(){
MyObject one(1,1);
MyObject two(2,2);
MyObject three(3,3);
one = two = three;
cout << one.x << ", " << one.y;
cout << two.x << ", " << two.y;
cout << three.x << ", " << three.y;
}
这样做,一、二、三中的变量 x 和 y 不变。我知道我应该更新 MyObject 的成员变量并使用按引用返回并返回 *this 以获得正确的行为。但是, one = two = three 中的返回值实际发生了什么? return temp 实际上在链中的哪个位置结束,例如一步一步?
【问题讨论】:
标签: c++ overloading operator-keyword