* 只有return *this才能实现连续赋值。
赋值的黑科技写法(赋值采用右结合律):
int x,y,z;
x = y = z = 4;
如何实现上述的连续赋值?
class Add{
public:
...
Add& operator= (const Add& add)
{
...
return *this; //传入的实参为等号右侧的值,返回的刚好是左侧的引用。
}
};
连续赋值黑科技:类Add的右结合赋值操作
* 只有return *this才能实现连续赋值。
赋值的黑科技写法(赋值采用右结合律):
int x,y,z;
x = y = z = 4;
如何实现上述的连续赋值?
class Add{
public:
...
Add& operator= (const Add& add)
{
...
return *this; //传入的实参为等号右侧的值,返回的刚好是左侧的引用。
}
};
175
851

被折叠的 条评论
为什么被折叠?