Effective C++ 条款10
2017-06-11 15:03
267 查看
令operator=返回一个reference to *this
将operator=返回一个reference是为了什么呢?答案非常easy,就是为了实现连锁形式。什么是连锁形式。如int x。y,z。x=y=z=15;这样的形式就是连锁形式。
声明一下。这仅仅是个大家一致允许的写法。你也能够不遵守这样的写法。
但是不管是内置类型还是标准库的类型,都遵循这条规则。
为了达到程序的一致性。也是遵守的比較好。
下面是涉及的代码:
#include<iostream> using namespace std; class Widget { public: Widget() { cout<<"Default Ctor"<<endl; } Widget(const Widget& rhs) { cout<<"Copy Ctor"<<endl; } Widget& operator=(const Widget& rhs) { cout<<"operator="<<endl; return *this; } }; int main() { Widget a,b,c; a=b=c; return 0; }
相关文章推荐
- Effective C++ -----条款10: 令operator=返回一个reference to *this
- Effective C++ 条款10
- Effective c++学习笔记——条款10:令operator=返回一个*this的引用
- Effective C++:条款10:令operator=返回一个reference to *this。
- Effective C++ 条款10 令operator=返回一个reference to *this 条款11 在operator=中处理"自我赋值"
- 《Effective C++》学习笔记条款10 令operator= 返回一个reference to *this
- 读书笔记《Effective C++》条款10:令operator=返回一个reference to *this
- Effective C++ 总结2 内存管理 (条款5 - 10)
- 《Effective C++》构造/析构/赋值 函数:条款10-条款12
- effective c++ 条款10 handle assignment to self operator =
- effective C++ 条款 10:operator= 返回一个reference to *this
- effective C++ 读书笔记 条款10
- 【effective c++】条款10:令operator=返回一个reference to *this
- Effective C++ 条款10解读: 令operator= 返回一个reference to *this
- Effective C++ 条款10 令赋值操作符operator=返回一个引用,指向*this
- Effective C++ 条款10
- 《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