《Effective C++ 》学习笔记——规定10
***************************************转载请注明出处:http://blog.csdn.net/lttree********************************************
二、Constructors,Destructors and Assignment Operators
Rule 10:Have assignment operators return a reference to *this
规则10:令operator= 返回一个 reference to *this
关于赋值,有一个非常有趣的连锁代码:
int x,y,z;
x = y = z = 15; // 赋值的连锁形式
并且,C++採用右结合律,假设用括号来代表优先计算顺序。就是这种:
x = ( y = ( z = 15 ) ) ;
C++中,为了实现“连锁赋值”,赋值操作符必须返回一个reference指向操作符的左側实參。
(注意。这是classes 实现赋值操作符时应该遵循的协议)
<span style="font-size:14px;">class Widget {
public:
...
Widget& operator=( const Widget& rhs )<span style="white-space: pre;"> </span>// 返回类型是个reference
{
...
return* this;<span style="white-space: pre;"> </span>// 返回左側对象
}
...
};</span>
并且这个协议不仅适用于以上的标准赋值形式,也适用于全部赋值相关的运算。比方+=、-=、*= 等:
Widget& operator+=( const Widget& rhs )
{
...
return *this;
}
可是,要注意这仅仅是一个协定。并无强制性,假设不遵循它,代码一样能够通过编译。
然而这份协议被全部内置类型和标准程序库提供的类型,如 string、vector、complex,trl::shared_ptr 等共同遵守。
所以除非有足够的理由去另类一下,不然还是从众吧。
☆请记住
令 assignment(赋值) 操作符返回一个 reference to *this。
***************************************转载请注明出处:http://blog.csdn.net/lttree********************************************
《Effective C++ 》学习笔记——规定10的更多相关文章
- Effective STL 学习笔记 32 ~ 33
Effective STL 学习笔记 32 ~ 33 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
- Effective STL 学习笔记: Item 22 ~ 24
Effective STL 学习笔记: Item 22 ~ 24 */--> div.org-src-container { font-size: 85%; font-family: monos ...
- Effective STL 学习笔记 Item 21:Comparison Function 相关
Effective STL 学习笔记 Item 21:Comparison Function 相关 */--> div.org-src-container { font-size: 85%; f ...
- 【python学习笔记】10.充电时刻
[python学习笔记]10.充电时刻 任何python都可以作为模块倒入 *.pyc:平台无关的经过编译的的python文件, 模块在第一次导入到程序中时被执行,包括定义类,函数,变量,执行语句 可 ...
- Effective STL 学习笔记 39 ~ 41
Effective STL 学习笔记 39 ~ 41 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
- Effective STL 学习笔记 Item 38 : Design functor classes for pass-by-value
Effective STL 学习笔记 Item 38 : Design functor classes for pass-by-value */--> div.org-src-container ...
- Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据
Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据 */--> div.org-src-container { font-size: 85%; font-fam ...
- Effective STL 学习笔记 31:排序算法
Effective STL 学习笔记 31:排序算法 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
- Effective STL 学习笔记 Item 30: 保证目标区间足够大
Effective STL 学习笔记 Item 30: 保证目标区间足够大 */--> div.org-src-container { font-size: 85%; font-family: ...
- Effective STL 学习笔记 Item 26: Prefer Iterator to reverse_iterator and const_rever_itertor
Effective STL 学习笔记 Item 26: Prefer Iterator to reverse_iterator and const_rever_itertor */--> div ...
随机推荐
- error: /usr/include/stdio.h: Permission denied 的一种情况分析
error: /usr/include/stdio.h: Permission denied 的一种情况分析 代码: #include <stdio.h> int main(){ prin ...
- c/c++ unix ipc
c/c++ unix ipc 一个例子 //c_unix.c #include <stdio.h> #include <sys/types.h> #include <sy ...
- POJ 2184 Cow Exhibition 01背包
题意就是给出n对数 每对xi, yi 的值范围是-1000到1000 然后让你从中取若干对 使得sum(x[k]+y[k]) 最大并且非负 且 sum(x[k]) >= 0 sum(y[k] ...
- 《深入理解mybatis原理》 MyBatis事务管理机制
MyBatis作为Java语言的数据库框架,对数据库的事务管理是其很重要的一个方面.本文将讲述MyBatis的事务管理的实现机制. 首先介绍MyBatis的事务Transaction的接口设计以及其不 ...
- Vijos P1881 闪烁的星星
背景 星光闪耀--深蓝色空间 听说过他们的语言 沉默 暮 他们称赞深相互 描写叙述 繁星, 漫天的繁星. 繁星排成一列, 我数一数呀, 一共同拥有N仅仅小星星呢. 星星们是听话的好孩子, 小岛在指挥它 ...
- thinkphp中URL传参数的几种方式
在thinkphp中,url传参合asp.net中原理类似,下面就单个参数和多个参数传递方式进行一个简单讲解 1.传单个参数 单个参数这种比较简单,例如 想像edit操作里面传递一个id值,如下写法_ ...
- UVAlive 2519 Radar Installation (区间选点问题)
Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. ...
- Mod in math
An Introduction to Modular Math When we divide two integers we will have an equation that looks like ...
- JavaScript(15)jQuery 选择器
jQuery 选择器 选择器同意对元素组或单个元素进行操作. jQuery 元素选择器和属性选择器同意通过标签名.属性名或内容对 HTML 元素进行选择. 在 HTML DOM 术语中:选择器同意对 ...
- linux进程通信之信号
本节主要学习信号和与信号相关的处理函数,兴许还会更新. http://blog.csdn.net/xiaoliangsky/article/details/40264151 一 信号 信号是UNIX和 ...