effective c++ 条款11 Handle assignment to self in operator=
2014-07-07 02:40
483 查看
赋值给自己,听起来有些不可思议,但是却要引起重视,它很容易把自己隐藏起来。
例如
1 a[i]=a[j];
如果 i, j的值一样?
2 *px=*py;
如果px py指向同一个object
3 rb and pd migh also be a same object.
以上表明多种自我赋值的可能性。
那么在自我赋值时会出现哪些问题呢?
表面看上述代码没有问题,但是如果自我赋值就会有问题。
1 自己指向的bitmap先被删除了。
2 另外这也可能导致异常安全性的问题。如果new的Bitmap没有成功,就会有一个指向未知地址的指针,无法删除而且无法读取。
我们先focus在问题1上面,问题2会在第29章详细讨论。
一个可行的做法
这会导致代码增大和降低执行速度(增加的代码,引入控制分支)
另外一个可行的办法是使用所谓的copy and swap技术
例如
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; }
相关文章推荐
- Effective C++ 11. Handle assignment to self in operator=
- 条款11:在operator=中处理“自我赋值”(Handle assignment to self in operator=.)
- 【条款11】Handle assignment to self in operator=
- effective c++ 11: Handle assignment to self in operator =
- Item 11: Handle assignment to self in operator=(Effective C++)
- effective c++ 条款10 handle assignment to self operator =
- Effective C++ Item 10,11 Have assignment operators return a reference to *this Handle assignment to self in operator =
- Item11: Handle assignment to self in operator=
- Effective C++, 3rd Edition, Item 11: 在 operator= 中处理 assignment to self(自赋值)
- [翻译] Effective C++, 3rd Edition, Item 11: 在 operator= 中处理 assignment to self(自赋值)
- Effective C++ 条款九、十 绝不在构造和析构过程中调用virtual函数|令operator=返回一个reference to *this
- 条款10:令operator=返回一个reference to * this(Have assignment operators return a reference to *this)
- Effective C++ -----条款11: 在operator=中处理“自我赋值”
- Effective C++ 条款10:令operator= 返回一个reference to *this
- Effective C++ 条款11:在operator=中处理"自我赋值"
- Effective C++ 条款10: 令operator= 返回一个reference to *this
- 【Effective c++】条款11:在operator=中处理“自我赋值”
- 《Effective C++》学习笔记条款10 令operator= 返回一个reference to *this
- Effective C++:条款10:令operator=返回一个reference to *this。
- Effective C++:条款11:在operator= 中处理“自我赋值”。