effective c++ 条款10:令operator=返回一个reference to *this

记住:
为实现“连锁赋值”,赋值操作符必须返回一个reference指向操作符的左侧实参。

class Widget {
public:
    ...
    Widget& operator=(const Widget& rhs)
    {
        ...
        return *this; //返回左侧对象
    }
    Widget& operator+=(const Widget& rhs)
    {
        ...
        return *this;
    }
    Widget& operator=(int rhs)
    {
        ...
        return *this;
    }
};

 

posted @ 2018-06-15 10:04  pfsi  阅读(136)  评论(0编辑  收藏  举报