”自我賦值”發生在對象被賦值給自己時:
class Widget { ... };
Widget w;
...
w = w; // 賦值給自己
a[i] = a[j]; // 潛在的自我賦值
*px = *py; // 潛在的自我賦值class Base { ... };
class Derived: public Base { ... };
void doSomething(const Base& rb, Derived* pd); // rb和*pd有可能其實是同一對象
嘗試自我管理資源
class Bitmap { ... };
class Widget {...
private:Bitmap* pb; // 指針,指向一個從heap分配而得的對象
};
// 一個不安全的operator=實現版本
Widget& Widget::operator=(const Widget& rhs) {delete pb; // 停止使用當前的bitmappb = new Bitmap(*rhs.pb); // 使用rhs's bitmap的副本return *this; // 見條款10
}
可能出現的問題就是,*this和rhs可能是同一個對象,最后會導致自己持有一個指針指向一個已被刪除的對象!
自我安全性
Widget& Widget::operator=(const Widget& rhs) {if (this == rhs) return *this; // 證同測試// 如果是自我賦值,就不做任何事情delete pb;pb = new Bitmap(*rhs.pb);return *this;
}
以上代碼確實解決了自我賦值的問題,但是會引發異常的問題,如果new Bitmap導致異常了,那么Widget最終會持有一個指針指向一塊被刪除的Bitmap。
異常安全性
Widget& Widget::operator=(const Widget& rhs) {Bitmap* pOrig = pb; // 記住原先的pbpb = new Bitmap(*rhs.pb);delete pOrig; // 刪除原先的pbreturn *this;
}
copy and swap
class Widget {
...
void swap(Widget& rhs);
...
};
Widget& Widget::operator=(const Widget& rhs) {Widget temp(rhs);swap(temp);return *this;
}
Widget& Widget::operator=(Widget rhs) { // 注意這里是傳值swap(rhs); return *this;
}
總結
- 確保當對象自我賦值時operator=有良好行為。其中技術包括比較”來源對象”和”目標對象”的地址、精心周到的語句順序、以及copy-and-swap.
- 確定任何函數如果操作一個以上的對象,而其中多個對象是同一個對象時,其行為仍然正確。