effect C++ 在operatro = 中处理“自我赋值”

来源:互联网 发布:连锁店用什么软件 编辑:程序博客网 时间:2024/05/22 07:40

自我赋值

自我赋值发生在对象被赋值给自己时:

class Widght{...}Widget w;...w=w;   //赋值给自己

赋值动作并不总是那么可被一眼识别出来。

a[i]=a[j];   //如果i和j有相同的值*px=*py;     //如果px和py恰巧指向同一个东西

如果你尝试自行管理资源,可能会掉进“在停止使用之前意外释放了它的陷阱”。

假设你建立一个class用来保存一个指针指向的一块动态分配的位图:

class Bitmap{...}class Widget{ ...private:  Bitmap *pb;//指向一个从heap分配而得的对象}Widget&Widget::operator=(const Widget& rhs) //不安全的operator={    delete pb;    pb=new Bitmap(*rhs.pb);    return *this;}

operator=函数内的*this 和rhs 有可能是同一个对象。这样delete就不只是销毁当前的bitmap,它也销毁rhs的bitmap。


传统的做法是加一个“证同测试”

Widget&Widget::operator=(const Widget& rhs) //不安全的operator={    if(this==&rhs)return *this;    delete pb;    pb=new Bitmap(*rhs.pb);    return *this;}


但是,如果“new Bitmap”导致异常(不论是因为分配时内存不足或是因为Bitmap的copy构造函数抛出异常),Widget最终会持有一个指针指向一块被删除的Bitmap.

以下代码,我们只需要注意在复制pb所指东西之前别删除pb:

Widget&Widget::operator=(const Widget& rhs) {     Bitmap* pOrig =pb;//记住原先的pb     pb=new Bitmap(*rhs.pb);// 令pb指向*pb的一个复件    delete pOrig;//删除原先的pb     return *this;}

现在,如果“newBitmap”抛出异常,pb保持原状。


也可以使用copy and swap

class Bitmap{...}class Widget{ ... void swap (Widget& rhs);//交换*this 和 rhs 的数据 ...}Widget&Widget::operator=(const Widget& rhs) {    Widget temp(rhs);//为rhs数据制作一份复件    swap(temp);                 //将*this数据和上述复件的数据交换     return *this;}

确保当对象自我赋值时operator= 有良好行为。其中技术包括比较“来源对象”和“目标对象”的地址、精心周到的语句顺序、以及copy-and-swap。

确定任何函数如果操作一个以上的对象,而其中多个对象是同一个对象时,其行为仍然正确。

0 0
原创粉丝点击