10、令operate= 返回一个reference to *this
来源:互联网 发布:淘宝评分影响 编辑:程序博客网 时间:2024/04/30 16:48
先看看一个连续赋值的例子:
int x, y, z;
x = y = z = 3;
由于赋值时采用右结合的,即相当于:
x = (y = (z = 3));
3先赋值给z,然后其结果(更新后的z)再赋值给y,然后其结果(更新后的y)再赋值给x;所以为了实现“连续赋值”,赋值操作符必须返回一个reference指向操作符的左侧的实参。这也是你为classes实现赋值操作符是时应该遵循的协议;
class Widget
{
public:
.......
Widget& operate=(const Widget& ref) //返回类型是个引用,指向当前对象
{
return this; //返回左侧对象;
}
}
像*=,+=所有使用与赋值操作的运算符都最好遵循这个协议。
0 0
- 10、令operate= 返回一个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: 令operator=返回一个reference to *this
- effective c++ 令operator=返回一个reference to *this
- 令operator=返回一个reference to *this
- 令operator= 返回一个reference to this
- 【寒江雪】令operator=返回一个reference to *this
- 工作日志2014-08-06
- zookeeper注册中心解析
- 对象的初始化
- SQL拼接代码!cmstop系统
- A/B(乘法逆元)
- 10、令operate= 返回一个reference to *this
- ActiveReport入门教程(三) - 卡片组
- zeromq源码阅读 —— Msg.hpp & Msg.cpp 的 msg_t (zmq中的消息)
- JS操作DOM元素属性和方法(转)
- 博弈论, 取石子游戏1
- linux系统下如何解压RAR文件软件rarforlinux
- FIFO队列并发同步访问 C++ 实现 ~~~
- android中根据touch事件判断单击及双击
- 使用uploadPreview插件上传文件并且预览