/*
自定义类一般会重载等号运算符,为可以实现连等操作
所以会返回*this的引用
*/
class Widget{
public:
Widget& operator=(const Widget& rhs)//=,+=.-=,*=这些类似的操作都可以用
{ //返回*this的引用来实现
return *this;
}
Widget& operator+=(const Widget& rhs)
{
return *this;
}
Widget& operator-=(const Widget& rhs)
{
return *this;
}
Widget& operator*=(const Widget& rhs)
{
return *this;
}
Widget& operator=(Widget& rhs) //参数与上面的不一致也可以这么用
{
return *this;
}
};
effective C++学习---条款10:令operator=返回一个reference to *this
最新推荐文章于 2021-09-24 14:49:36 发布