日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Effective C++ 条款11:在operator=中处理自我赋值

發布時間:2023/12/13 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Effective C++ 条款11:在operator=中处理自我赋值 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

”自我賦值”發生在對象被賦值給自己時:

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.
  • 確定任何函數如果操作一個以上的對象,而其中多個對象是同一個對象時,其行為仍然正確。
  • 轉載于:https://www.cnblogs.com/zhonghuasong/p/7443800.html

    總結

    以上是生活随笔為你收集整理的Effective C++ 条款11:在operator=中处理自我赋值的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。