Effective C++ Item 10,11 Have assignment operators return a reference to *this Handle assignment to self in operator =
If you want to concatenate assignment like this
int x, y, z;
x = y = z = 15;
The convention is to make the assignment operators return a reference to *this.
11 Handle assignmnet to self in operator =
You can easily assign an object to itself without knowing it
// Example 1
Foo *px, *py;
px = py; //Example 2
a[i] = a[i];
It is dangerous to assign an object to self if your class doesn't handle this kind of opeator appropriately. Let's consider an example here:
class Bitmap{...};
class Widget {
...
private:
Bitmap *m_pb;
};
Widget& Widget::operator=(const Widget& rhs) {
delete m_pb;
m_pb = new Widget(rhs);
return *this;
}
You will be dead if you assign an object to itself, beacause you will end up holding a pointer to a deleted object!
One way but not the best way to deal with self assignment is to write code to explicitly deal with this situation:
Widget& Widget::operator=(const Widget& rhs) {
if(this == &rhs) {
return *this;
}
delete m_pb;
m_pb = new Bitmap(*rhs).m_ph;
return *this;
}
This is not the best way because above this operator is not exception-safe. What does that mean? Imagine what if an exception is thrown when
ph = new Bitmap(*rhs.m_ph)
is been executed(for example, not enough memory), then the object will hold a pointer to a deleted object.
Luckily, making operator= safe renders it self-assignmnet-sage, too. So, the best way to accomplish that is:
Widget& Widget::operator=(const Widget& rhs) {
Bitmap* tmp = m_pb;
pb = new Bitmap(*rhs.m_pb);
delete tmp;
return *this;
}
Now if "new Bitmap" throw an exception, pb remains unchanged. Even without the identity test, this code handle assignment to self, because we make a copy of the original bitmap, point to the copy we made, and then delete the original Bitmap.
A more straightforward way to do so is to use exception-safe function swap
Widget& Widget::operator=(const Widget& ths) {
Widget temp(ths);
swap(temp);
return *this;
}
Effective C++ Item 10,11 Have assignment operators return a reference to *this Handle assignment to self in operator =的更多相关文章
- 条款10:令operator=返回一个reference to * this(Have assignment operators return a reference to *this)
NOTE: 1.令赋值(assignment)操作符返回一个reference to *this. 2.此协议适用于所有赋值相关的运算比如:+= -= *=....
- Effective JavaScript Item 10 避免使用with
本系列作为Effective JavaScript的读书笔记. Item 9:避免使用withkeyword 重点: 设计withkeyword本来是为了让代码变简洁,可是却起到了相反的效果.比方: ...
- 读书笔记 effective c++ Item 10 让赋值运算符返回指向*this的引用
一个关于赋值的有趣的事情是你可以将它们链在一起: int x, y, z; x = y = z = ; // chain of assignments 同样有趣的是赋值采用右结合律,所以上面的赋值链被 ...
- <Effective C++>读书摘要--Ctors、Dtors and Assignment Operators<二>
<Item 9> Never call virtual functions during construction or destruction 1.you shouldn't call ...
- <Effective C++>读书摘要--Ctors、Dtors and Assignment Operators<一>
<Item 5> Know what functions C++ silently writes and calls 1.If you don't declare them yoursel ...
- 读书笔记 effective c++ Item 11 在operator=中处理自我赋值
1.自我赋值是如何发生的 当一个对象委派给自己的时候,自我赋值就会发生: class Widget { ... }; Widget w; ... w = w; // assignment to sel ...
- 读书笔记 effective c++ Item 5 了解c++默认生成并调用的函数
1 编译器会默认生成哪些函数 什么时候空类不再是一个空类?答案是用c++处理的空类.如果你自己不声明,编译器会为你声明它们自己版本的拷贝构造函数,拷贝赋值运算符和析构函数,如果你一个构造函数都没有声 ...
- 读书笔记 effective c++ Item 12 拷贝对象的所有部分
1.默认构造函数介绍 在设计良好的面向对象系统中,会将对象的内部进行封装,只有两个函数可以拷贝对象:这两个函数分别叫做拷贝构造函数和拷贝赋值运算符.我们把这两个函数统一叫做拷贝函数.从Item5中,我 ...
- Java基础-赋值运算符Assignment Operators与条件运算符Condition Operators
Java基础-赋值运算符Assignment Operators与条件运算符Condition Operators 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.赋值运算符 表 ...
随机推荐
- 工作总结 表单提交中 Input 设置 disabled readonly
input框里面添加disabled属性之后,该内容就无法向上提交 需要的时候也可以再移除disabled readonly这个属性来禁止用户修改, 可以正常提交. Hiddent 隐藏 也可以正 ...
- python如何获取公众号下面粉丝的openid
如何获取公众号下面粉丝的openid呢,首先要获取一个access_token,这个token可不是令牌(Token),如何获取这个access_token呢?有两种方法,方法如下: # -*- co ...
- Mysql5.7的初始密码更改
软件版本的变化真是让人兴奋…… Linux服务器决定安装使用mysql 5.7了. 愉快的去官网下载安装包:https://dev.mysql.com/downloads/mysql/ 解决完所有依赖 ...
- 5V系统和3.3V系统电平转换
在设计一个带MCU或者ARM系统电路时候,经常遇见MCU的VCC是3.3V,但是外围电路需要5V.有时候是反过来.虽然现在MCU的IO都声称支持TTL电平,但是我们谁也不想将MCU的IO口直接接上5V ...
- EAST 自然场景文本检测
自然场景文本检测是图像处理的核心模块,也是一直想要接触的一个方面. 刚好看到国内的旷视今年在CVPR2017的一篇文章:EAST: An Efficient and Accurate S ...
- Hexo快速搭建静态博客并实现远程VPS自动部署
这篇文章将如何搭建hexo,以及如何通过git webhooks实现远程vps的自动部署 这篇文件适合的条件: 简单的用于个人博客.公司博客展示,hexo的定位是静态博客,要实现动态服务器的功能并不适 ...
- Experience on Namenode backup and restore --- checkpoint
Hadoop version: Hadoop 2.2.0.2.0.6.0-0009 Well, We can do this by building Secondary Namenode, Check ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- python学习之yummain模块
定义:`yum`的命令行接口. yummain.main(args) Run the yum program from a command line interface. yummain.hotsho ...
- 安装CentOS版本的yum(转载)
安装CentOS版本的yum 下载源:http://mirrors.163.com/centos/6/os/i386/Packages/ 材料准备: python-iniparse-0.3.1-2.1 ...