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;    
}

 

 

posted @ 2014-07-07 02:40  williamwood  阅读(249)  评论(0编辑  收藏  举报