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

这样做是为了能连等于,下面是个例子。

class Number{
public:
    Number& operator=(int num){
        number = num;
        return *this;
    }
private:
    int number;
};

int main() {
    Number a, b, c;
    a = b = c = 1;
    a = (b = (c = 1));
}

像+=,*= ,/=这种带有=性质的操作符,最好都返回一个*this

上一篇:iOS开发之UITapGestureRecognizer单双击


下一篇:JAVA学习笔记 -- 多线程之共享资源