十、重载赋值操作符时返回对this指针的引用

  • 对于所有形式的赋值运算,返回对*this指针的引用总是对的,没什么好说的,实现连续赋值x = y = z = 10。

    class A {
        private:
            int x;
            int y;
            int z;
    
        public:
            A& operator=(const A& rhs)    //标准形式的赋值操作
            {
                ……
                return* this;
            }
    
            A& operator+=(int rhs)        //非标准形式的赋值操作
            {
                ……
                return* this;
            }    
    
杜鹏
2012-08-23