【发布时间】:2017-01-01 17:34:31
【问题描述】:
非常直接的问题。
class Bitmap {...};
class Widget {
...
private:
Bitmap* pb;
};
当重载复制赋值时,书(Effective C++)说:下面的代码是异常安全的。
Widget& Widget::operator=(const Widget& rhs) {
if (rhs == *this) return;
Bitmap* pOrig = pb; //?? why remember the pb can do exception safety?
pb = new Bitmap(*rhs.pb);
delete pOrig;
return *this;
}
书上说:即使通过new Bitmap(*rhs.pb)遇到异常,上面的代码也可以做到异常安全,pb可以保持不变,不是指针指向NULL?但是如何,为什么?有人可以帮忙我?谢谢!
【问题讨论】:
-
是这本书的确切内容吗?这听起来不像是那种可以通过编辑完成的文本。如果可能,请引用确切的文字。
-
什么样的异常安全?强的?基本的?没有?
-
我怀疑这本书说的是
&rhs == this,而不是rhs == *this。 -
是的!是我的错!谢谢!