Effective C++ 条款11:在operator=中处理自我赋值
生活随笔
收集整理的這篇文章主要介紹了
Effective C++ 条款11:在operator=中处理自我赋值
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
”自我賦值”發(fā)生在對象被賦值給自己時:
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=實現(xiàn)版本 Widget& Widget::operator=(const Widget& rhs) {delete pb; // 停止使用當前的bitmappb = new Bitmap(*rhs.pb); // 使用rhs's bitmap的副本return *this; // 見條款10 }可能出現(xiàn)的問題就是,*this和rhs可能是同一個對象,最后會導致自己持有一個指針指向一個已被刪除的對象!
自我安全性
Widget& Widget::operator=(const Widget& rhs) {if (this == rhs) return *this; // 證同測試// 如果是自我賦值,就不做任何事情delete pb;pb = new Bitmap(*rhs.pb);return *this; }以上代碼確實解決了自我賦值的問題,但是會引發(fā)異常的問題,如果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; }總結
轉載于:https://www.cnblogs.com/zhonghuasong/p/7443800.html
總結
以上是生活随笔為你收集整理的Effective C++ 条款11:在operator=中处理自我赋值的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小米MIX Fold 2在部分门店抢购一
- 下一篇: Epic喜+1:《烹调、上菜、美味3》