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 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.赋值运算符 表 ...
随机推荐
- Kubuntu 初始配置
1.中文配置 系统设置中地区选择中国.语系使用简体中文(一開始可能仅仅有英文选项,似乎是更新后能够选择其它语言了) 安装中文字体: sudo apt-get install ttf-wqy-micro ...
- 在eclipse中将android工程打包生成apk文件
1.)生成keystore 按照下面的命令行 在C:\Program Files\Java\jdk1.6.0_10\bin>目录下,输入keytool -genkey -alias androi ...
- 使用spring AOP获得session的思路
由于Spring 的AOP面向切面编程,与Servlet容器没有任何关联,所以想要获得Session会话比较麻烦. 当然Struts2同样不依赖Servlet容器,可以在Spring AOP中可以使用 ...
- 【Unity】开发WebGL内存概念具体解释和遇到的问题
自增加unity WebGL平台以来.Unity的开发团队就一直致力于优化WebGL的内存消耗. 我们已经在Unity使用手冊上有对于WebGL内存管理的详尽分析,甚至在Unite Europe 20 ...
- django.db.utils.OperationalError: no such table: auth_user
关于使用django 首次创建超级管理员时,出现 django.db.utils.OperationalError: no such table: auth_user 错误 1.首先使用命 ...
- layui 数据表格 根据值(1=业务,2=机构)显示中文名称
数据是用ThinkPHP5操作 类型是固定4个, 用layui templet - 自定义模板 方法一: {field:'type', title: '类型', width: 200, templet ...
- J2EE学术交流感悟——分层
学术交流进行了一周,是关于J2EE的学术报告. 目的是让我们在学习的时候对"所学知识"有一个宏观的认识. 開始是以为环绕"J2EE"进行解说,怕自己没有 ...
- iOS之美: UIView 与 UIWindow之间的关系
转自:http://leopard168.blog.163.com/blog/static/168471844201381584533466/ 面对iOS初学者,总会被问到一些不常被关注的问题,比如: ...
- 安装CentOS7后,无法联网,用yum安装软件提示 cannot find a valid baseurl for repo:base/7/x86_64 的解决方法
无法联网的明显表现会有: 1.yum install出现 Error: cannot find a valid baseurl or repo:base 2.ping host会提示unknown h ...
- UIApplication深入学习
多时候,我们不需要关心这个类,我们很少继承这个类,偶尔会调用这个类的api来实现一些功能,但是不可否认,这个类是iOS编程中很重要的一个概念.UIApplication的核心作用是提供了iOS程序运行 ...