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;
    }
};

猜你喜欢

转载自www.cnblogs.com/pfsi/p/9186122.html