effective c++ 条款11 Handle assignment to self in operator=

赋值给自己,听起来有些不可思议,但是却要引起重视,它很容易把自己隐藏起来。

 

例如

1  a[i]=a[j];

  如果 i, j的值一样?

2  *px=*py;

  如果px py指向同一个object

3    rb and pd migh also be a same object.

class Base
{
  ... 
};
class Derived : public Base
{
  ...
};
void doSomething(const Base& rb, Derived *pd);

以上表明多种自我赋值的可能性。

那么在自我赋值时会出现哪些问题呢?

class Bitmap{};
class Widget
{
  private:
    Bitmap* pb; // point to a mem from heap
}

Widget Widget::&operator=(const Widget & rhs)
{
     delete pb;
     pb = new Bitmap(*rhs.pb);
     return *this;     
}

表面看上述代码没有问题,但是如果自我赋值就会有问题。

1 自己指向的bitmap先被删除了。

2 另外这也可能导致异常安全性的问题。如果new的Bitmap没有成功,就会有一个指向未知地址的指针,无法删除而且无法读取。

我们先focus在问题1上面,问题2会在第29章详细讨论。

一个可行的做法

Widget Widget::&operator=(const Widget & rhs)
{
     if(this == &rhs) return *this;//identity test;
     
     delete pb;
     pb = new Bitmap(*rhs.pb);
     return *this;     
}

这会导致代码增大和降低执行速度(增加的代码,引入控制分支)

另外一个可行的办法是使用所谓的copy and swap技术

Widget Widget::&operator=(const Widget & rhs)
{
    Widget tmp(rhs);
    swap(tmp);  // swap *this and tmp
    return *this;    
}

 

 

转载于:https://www.cnblogs.com/williamwood/p/3828963.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值