13.6 重載了Operator=,對(duì)象是相同類型的運(yùn)算符 當(dāng)用一個(gè)對(duì)象對(duì)另一個(gè)對(duì)象賦值的時(shí)候使用 類似于基礎(chǔ)類型的賦值,把符號(hào)右邊的賦值給左邊 當(dāng)類內(nèi)沒有定義的時(shí)候,編譯器會(huì)合成自己的版本
13.7 會(huì)發(fā)生淺拷貝,這種情況是不允許的
13.8
#include <string>class Hasptr {public: HasPtr(const std::string &s = std::string()) : ps(new std::string(s)), i(0) { } HasPtr(const HasPtr& hp) : ps(new std::string(*hp.ps)), i(hp.i) { } HasPtr& operator=(const HasPtr& rhs){ if(this=&rhs) return this; delete ps; ps=new string (*rhs.ps); i=rhs.i; return *this;}PRivate: std::string *ps; int i;};新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注