effective C++ 讀書筆記 條款11,effective讀書筆記

來源:互聯網
上載者:User

effective C++ 讀書筆記 條款11,effective讀書筆記

條款11: 在operator= 中處理“自我賦值”

 

在實現operator=時考慮自我賦值是必要的就像 x=y ,我們不知道變數x與y代表的值是否為同一個值(把x和y說成是一個指標更恰當一點)。如下

 

第一版:

#include <iostream>using namespace std;class bitmap{public:bitmap(){cout<<"調用bitmap()無參建構函式"<<endl;}bitmap(const bitmap& bt){this->i = bt.i;cout<<"調用bitmap()拷貝建構函式"<<endl;}~bitmap(){}private:int i;};class Widget{public:Widget(){pb = new bitmap();}Widget(const Widget& wd){this->pb = wd.pb;}public:Widget& operator=(const Widget& rhs);private:bitmap* pb; //定義一個從heap分配而得的對象};//第一版賦值函數:Widget& Widget::operator=(const Widget& rhs){delete pb;pb = new bitmap(*rhs.pb);return *this;}//這一版函數的pb在使用前清理掉之前的pb指向,再接受一個new出來的新對象,看著很順理成章,但是//當this與函數參數rhs相等的時候,pb = new bitmap(*rhs.pb);因為我們已經把*rhs.pb delete掉了。int main(){Widget w1;Widget w2;w1 = w2;return 0;}/*調用bitmap()無參建構函式調用bitmap()無參建構函式調用bitmap()拷貝建構函式Press any key to continue*/


第二版:

//第二版賦值函數:Widget& Widget::operator=(const Widget& rhs){if (this == &rhs){return *this;}delete pb;pb = new bitmap(*rhs.pb);return *this;}//這個版本行的通,賦值函數基本上是可以接受的,但是不見得是安全的,因為當new產生異常時pb依然是一個//不確定的指標。


第三版:

//第三版賦值函數:Widget& Widget::operator=(const Widget& rhs){bitmap *pOrig = pb;pb = new bitmap(*rhs.pb);delete pOrig;return *this;}//第三版函數在開始的時候用pOrig記錄了pb,當new沒有異常時我們再把pb原來的指向空間釋放掉//從而提高了安全性。這種方法也同樣能夠處理自我賦值,假如這裡rhs=*this;我們先對原來的//bitmap做了一份備份,刪除原bitmap後,指向了我們的那一份備份,或許這種處理自我賦值的方法//不是很好,但是行的通,在保證安全性的情況下採用這種辦法很不錯。


 

第四版:

#include <iostream>using namespace std;class bitmap{public:bitmap(){cout<<"調用bitmap()無參建構函式"<<endl;}bitmap(const bitmap& bt){this->i = bt.i;cout<<"調用bitmap()拷貝建構函式"<<endl;}~bitmap(){}private:int i;};class Widget{public:Widget(){cout<<"調用Widget()無參建構函式"<<endl;pb = new bitmap();}Widget(const Widget& wd){cout<<"調用Widget()拷貝參建構函式"<<endl;this->pb = wd.pb;}void my_swap(Widget& rhs);public:Widget& operator=(const Widget& rhs);private:bitmap* pb; //定義一個從heap分配而得的對象};//第四版賦值函數:void Widget::my_swap( Widget& rhs){pb = rhs.pb;}Widget& Widget::operator=(const Widget& rhs){Widget temp(rhs);//防止改變rhsmy_swap(temp);return*this;}//第四版賦值函數利用的是copy and swap技術,這個技術在條款29當中//有詳細說明,還沒認真看,這裡就不解釋了。//第四版也可以這樣用:Widget& Widget::operator=(Widget rhs){my_swap(rhs); //其實本質上是一樣的,因為傳遞的參數是值傳遞,所以這裡傳遞的是rhs的一個副本,相當於Widget temp(rhs);主要就是防止rhs被改變;return*this;}int main(){Widget w1;Widget w2;w1 = w2;return 0;}/*調用Widget()無參建構函式調用bitmap()無參建構函式調用Widget()無參建構函式調用bitmap()無參建構函式調用Widget()拷貝參建構函式Press any key to continue*/


總結:

1:確保當對象自我賦值時 operator= 有良好行為,其中技術包括比較“原來對象”和“目標對象”的地址。、精心周到的語句順序,以及copy and swap

2:確定任何函數如果操作一個以上的對象,而其中多個對象是同一個對象時,其行為仍然正確。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.