【effective c++】条款10:令operator=返回一个reference to *this
2012-08-30 10:53
591 查看
为了实现“连锁赋值”,赋值操作符必须返回一个reference指向操作符的左侧实参。这是你为类实现赋值操作符时应该遵循的协议:
注意,这只是个协议,并无强制性,如果不遵循它,代码一样可以通过编译。
令赋值操作符返回一个reference to *this。
class Widget { public: ... Widget& operator=(const Widget& rhs)//返回类型是reference { //指向当前对象 ... return *this; //返回左侧对象 } ... }; //这个协议不仅适用于以上标准赋值形式,也适用于所有赋值相关运算 class Widget { public: ... Widget& operator+=(const Widget& rhs) { ... return *this; } Widget& operator=(int rhs) { ... retrun *this;//此函数也适用,即使此操作的参数类型不符要求 } ... };
注意,这只是个协议,并无强制性,如果不遵循它,代码一样可以通过编译。
令赋值操作符返回一个reference to *this。
相关文章推荐
- effective C++ 条款 10:operator= 返回一个reference to *this
- 《Effective C++ 》条款10:令operator=返回一个reference to *this
- Effective C++ -----条款10: 令operator=返回一个reference to *this
- Effective C++ 条款10:令operator= 返回一个reference to *this
- Effective C++ 条款10解读: 令operator= 返回一个reference to *this
- 读书笔记《Effective c++》 条款10 令operator= 返回一个reference to *this
- Effective C++:条款10:令operator=返回一个reference to *this。
- 《Effective C++》学习笔记条款10 令operator= 返回一个reference to *this
- Effective C++ 条款10: 令operator= 返回一个reference to *this
- 读书笔记《Effective C++》条款10:令operator=返回一个reference to *this
- 条款10 令operator=返回一个reference to *this
- Effective C++ 10 令operator=返回一个reference to *this 笔记
- 条款10:令operator=返回一个reference to *this
- 《Effect C++》学习------条款10:令 operator= 返回一个 reference to *this
- 条款10:令operator= 返回一个reference to *this
- 条款10:令operator=返回一个reference to *this
- 条款10:令operator=返回一个reference to *this(Have assignment operators return a reference to *this.)
- Effective C++ 条款九、十 绝不在构造和析构过程中调用virtual函数|令operator=返回一个reference to *this
- 条款10:令operator=返回一个reference to *this
- 条款10:令operator= 返回一个reference to *this