class sample
{
private:
int radius;
float x,y;
public:
circle()
{
}
circle(int rr;float xx;float yy)
{
radius=rr;
x=xx;
y=yy;
}
circle operator =(circle& c)
{
cout << endl<<"Assignment operator invoked";
radius=c.radius;
x=c.x;
y=c.y;
return circle(radius,x,y);
}
}
int main()
{
circle c1(10,2.5,2.5);
circle c1,c4;
c4=c2=c1;
}
在重載「=」函數的聲明爲什麼重載操作符需要返回=對象?
radius=c.radius;
x=c.x;
y=c.y;
本身使所有C2的數據成員等於C1的,那麼,爲什麼是必要的回報? 類似地,在c1 = c2 + c3中,使用重載的+運算符添加c2和c3,並將該值返回給c1,但不會變爲c1 =,所以我們不應該使用another =運算符來分配c2和c3之和爲c1?我很困惑。
相關:http://stackoverflow.com/questions/2447696/overloading-assignment-operator-in-c和http://stackoverflow.com/questions/2649068/has-anyone-found-the-need-to -declare最返回參數的,一個拷貝賦值-O/2649576#2649576 – 2012-04-11 16:25:09