《Effective C++》读书笔记10:令operator=返回一个引用指向*this

一般的连锁赋值方式:

int x, y, z;

x = y = z = 15;//等价于x = (y = (z = 15));

当我们要实现自己的operator=操作时,就需要返回一个引用,该引用指向了操作符左侧的参数;

 1 class Widget
 2 {
 3 public:
 4   
 5   Widget& operator=(const Widget& rhs)
 6   {
 7     
 8     return *this;//返回*this
 9   }
10 };
这个*this是所以c++标准实现中的规范做法,不这样做也可以通过编译,不过既然是规范,就遵守一下吧

posted on 2009-02-26 22:12  月光笛手  阅读(355)  评论(0)    收藏  举报

导航