条款10:令operator=返回一个reference to *this(Have assignment operators return a reference to *this.)
来源:互联网 发布:mysql interval 编辑:程序博客网 时间:2024/04/28 16:03
1. 连锁赋值
int x, y, z;
x=y=z=15; //赋值连锁形式x=(y=(z=15));
为了实现“连锁赋值”,赋值操作符必须返回一个reference指向操作符的左侧实参。这是你为classes实现赋值操作符时应该遵
循的协议:
class Widget
{
public:
Widget& operator=(const Widget& rhs) //返回类型是个reference,指向当前对象。
{
return* this; //返回左侧对象
}
...
};
这个协议适用于+=,-,-=,*=等等。
2. 总结
1)令赋值(assignment)操作符返回一个reference to*this。
注:实验看到不返回*this,仍然可以支持连锁赋值。
- 条款10:令operator=返回一个reference to *this(Have assignment operators return a reference to *this.)
- 【条款10】Have assignment operators return a reference to *this
- 条款10:令operator= 返回一个reference to *this
- 条款10:令operator= 返回一个reference to *this
- 条款10:令operator=返回一个reference to *this
- 条款10:令operator = 返回一个reference to *this
- 条款10:令operator=返回一个reference to *this
- 条款10:令operator=返回一个reference to * this
- 条款10 令operator=返回一个reference to *this
- 条款10:令operator=返回一个reference to *this
- 条款10 令operator=返回一个 reference to *this
- 条款10 令operator=返回一个reference to *this
- 条款10:令operator=返回一个reference to *this
- 条款10:令operator=返回一个reference to *this
- Item10: Have assignment operators return a reference to *this
- 【effective c++】条款10:令operator=返回一个reference to *this
- 《Effective C++》学习笔记条款10 令operator= 返回一个reference to *this
- Effective C++——》条款10:令operator=返回一个reference to *this
- 自己写的一个JQuery自动完成例子
- 模拟实现 网络拥堵的状况 mina
- 拖放技术之一--52
- 如何学好嵌入式系统中的C语言编程
- ASP.NET 如何在后台动态加载 iframe 里的链接
- 条款10:令operator=返回一个reference to *this(Have assignment operators return a reference to *this.)
- 记录一下
- Android Activity和Intent用法实例
- Java操作excel!!
- AAA3.0
- 基于Linux的视频传输系统(转)
- 生成静态页好还是伪静态好,有何不同?
- 拖放技术之二
- JQuery优缺点