13.6 重載了Operator=,對象是相同類型的運算符 當用一個對象對另一個對象賦值的時候使用 類似于基礎類型的賦值,把符號右邊的賦值給左邊 當類內沒有定義的時候,編譯器會合成自己的版本
13.7 會發生淺拷貝,這種情況是不允許的
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;};新聞熱點
疑難解答