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 =的更多相关文章

  1. 条款10:令operator=返回一个reference to * this(Have assignment operators return a reference to *this)

    NOTE: 1.令赋值(assignment)操作符返回一个reference to *this. 2.此协议适用于所有赋值相关的运算比如:+= -= *=....

  2. Effective JavaScript Item 10 避免使用with

    本系列作为Effective JavaScript的读书笔记. Item 9:避免使用withkeyword 重点: 设计withkeyword本来是为了让代码变简洁,可是却起到了相反的效果.比方: ...

  3. 读书笔记 effective c++ Item 10 让赋值运算符返回指向*this的引用

    一个关于赋值的有趣的事情是你可以将它们链在一起: int x, y, z; x = y = z = ; // chain of assignments 同样有趣的是赋值采用右结合律,所以上面的赋值链被 ...

  4. <Effective C++>读书摘要--Ctors、Dtors and Assignment Operators<二>

    <Item 9> Never call virtual functions during construction or destruction 1.you shouldn't call ...

  5. <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 ...

  6. 读书笔记 effective c++ Item 11 在operator=中处理自我赋值

    1.自我赋值是如何发生的 当一个对象委派给自己的时候,自我赋值就会发生: class Widget { ... }; Widget w; ... w = w; // assignment to sel ...

  7. 读书笔记 effective c++ Item 5 了解c++默认生成并调用的函数

    1 编译器会默认生成哪些函数  什么时候空类不再是一个空类?答案是用c++处理的空类.如果你自己不声明,编译器会为你声明它们自己版本的拷贝构造函数,拷贝赋值运算符和析构函数,如果你一个构造函数都没有声 ...

  8. 读书笔记 effective c++ Item 12 拷贝对象的所有部分

    1.默认构造函数介绍 在设计良好的面向对象系统中,会将对象的内部进行封装,只有两个函数可以拷贝对象:这两个函数分别叫做拷贝构造函数和拷贝赋值运算符.我们把这两个函数统一叫做拷贝函数.从Item5中,我 ...

  9. Java基础-赋值运算符Assignment Operators与条件运算符Condition Operators

    Java基础-赋值运算符Assignment Operators与条件运算符Condition Operators 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.赋值运算符 表 ...

随机推荐

  1. java 判断日期是否是节假日

    1.新建一个excel命名为“节假日.xls”存放节假日,模板格式如下 2.判断是否是节假日的类 工作日返回true ,休息日返回false. 需要引用poi-bin-3.9包,包放在博客文件中 im ...

  2. 视频中的DTS与PTS的理解

    原文地址:http://blog.itpub.net/30004768/viewspace-1338882/  DTS(解码时间戳)和PTS(显示时间戳)分别是解码器进行解码和显示帧时相对于SCR(系 ...

  3. 列表的append方法和extend方法

    1.举例说明列表的append方法和extend l = ["zhy",666] l.extend(["edit","sdd"]) prin ...

  4. yii2 ContentDecorator 和 block 挂件

    在做网站的过程中,大部分的页面结构都是相似的.如都有相同的头部和底部.各个页面这样仅仅是中间的部分不同. Yii中的布局文件就是用来实现这样的功能.如: 布局文件:@app/views/layouts ...

  5. AngularJS 和 Electron 构建桌面应用

    译]使用 AngularJS 和 Electron 构建桌面应用 原文: Creating Desktop Applications With AngularJS and GitHub Electro ...

  6. Hystrix的原理与使用

    转载自:https://segmentfault.com/a/1190000005988895              http://blog.csdn.net/xiaoyu411502/artic ...

  7. na+mb与gcd

    蒜头君和花椰妹在玩一个游戏,他们在地上将 nn 颗石子排成一排,编号为 11 到 nn.开始时,蒜头君随机取出了 22 颗石子扔掉,假设蒜头君取出的 22 颗石子的编号为 aa, bb.游戏规则如下, ...

  8. java ssm框架入门(三)正式项目的web.xml配置

    一个正规的上线的web.xml的配置. <?xml version="1.0" encoding="UTF-8"?> <web-app id= ...

  9. poj3067 Japan(树状数组)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:id=3067">http://poj.org/problem? id=3067 Descri ...

  10. 实现itoa()

    上代码之前先讲个笑话:曾经有位面试官问:“你实现过 唉踢哦诶(音) 吗”? 我第一个想到的是各种OA系统,心想那玩意不多是Java实现的吗...过一会想明白了,瞬间石化... #include < ...