[Effective C++讀書筆記]0012_複製對象時勿忘其每一部分

來源:互聯網
上載者:User

第一段程式:

 1 #include <iostream> 2 using namespace std; 3  4 class Demo{ 5 public: 6     Demo(int value);                   // 預設建構函式 7     Demo(const Demo& rhs);             // 拷貝建構函式 8     Demo& operator=(const Demo& rhs);  // 重載賦值運算子 9     void printInfo();                 // 列印成員值函數10 private:11     int m_value;12 };13 14 Demo::Demo(int value){15     this->m_value = value;16 }17 18 Demo::Demo(const Demo& rhs):m_value(rhs.m_value){19     20 }21 22 Demo& Demo::operator=(const Demo& rhs){23     this->m_value = rhs.m_value;24     return *this;25 }26 27 void Demo::printInfo(){28     cout<<"object address: "<<this<<endl;29     cout<<"m_value = "<<this->m_value<<endl;30     cout<<"----------------------------"<<endl;31 }32 33 int main()34 {35     Demo  d1(10);    // 調用建構函式36     d1.printInfo();37 38     Demo d2(d1);     // 調用拷貝建構函式39     d2.printInfo();40 41     Demo d3(0);42     d3 = d1;         // 調用重載賦值操作符43     d3.printInfo();44 45     system("pause");46     return 0;47 }

輸出結果:

object address: 001AF9C4
m_value = 10
----------------------------
object address: 001AF9B8
m_value = 10
----------------------------
object address: 001AF9AC
m_value = 10
----------------------------
請按任意鍵繼續. . .

第二段程式: 

 1 #include <iostream> 2 using namespace std; 3  4 class Demo{ 5 public: 6     Demo(int value, char ch);         // 預設建構函式 7     Demo(const Demo& rhs);             // 拷貝建構函式 8     Demo& operator=(const Demo& rhs);  // 重載賦值運算子 9     void printInfo();                  // 列印成員值函數10 private:11     int m_value;12     char m_ch;                         // 注意這裡我追加了一個成員13 };14 15 Demo::Demo(int value, char ch){16     this->m_value = value;17     this->m_ch = ch;18 }19 20 Demo::Demo(const Demo& rhs):m_value(rhs.m_value){21     22 }23 24 Demo& Demo::operator=(const Demo& rhs){25     this->m_value = rhs.m_value;26     return *this;27 }28 29 void Demo::printInfo(){30     cout<<"object address: "<<this<<endl;31     cout<<"m_value = "<<this->m_value<<endl;32     cout<<"m_name = "<<this->m_ch<<endl;33     cout<<"----------------------------"<<endl;34 }35 36 int main()37 {38     Demo  d1(10, 'Z');    // 調用建構函式39     d1.printInfo();40 41     Demo d2(d1);     // 調用拷貝建構函式42     d2.printInfo();43 44     Demo d3(0, 'A');45     d3 = d1;         // 調用重載賦值操作符46     d3.printInfo();47 48     system("pause");49     return 0;50 }

輸出結果:

object address: 0017FCF4m_value = 10m_name = Z----------------------------object address: 0017FCE4m_value = 10m_name =----------------------------object address: 0017FCD4m_value = 10m_name = A----------------------------請按任意鍵繼續. . .

第三段程式:

 1 #include <iostream> 2 using namespace std; 3  4 class Base{ 5 public: 6     Base():b_value(0){} 7     Base(int value):b_value(value){} 8 protected: 9     int b_value;10 };11 12 class Demo : Base{13 public:14     Demo(int value);                   // 預設建構函式15     Demo(const Demo& rhs);             // 拷貝建構函式16     Demo& operator=(const Demo& rhs);  // 重載賦值運算子17     void printInfo();                  // 列印成員值函數18 private:19     int m_value;20 };21 22 Demo::Demo(int value){23     this->m_value = value;24     this->b_value = value;25 }26 27 Demo::Demo(const Demo& rhs):m_value(rhs.m_value){28     29 }30 31 Demo& Demo::operator=(const Demo& rhs){32     this->m_value = rhs.m_value;33     return *this;34 }35 36 void Demo::printInfo(){37     cout<<"object address: "<<this<<endl;38     cout<<"m_value = "<<this->m_value<<endl;39     cout<<"b_value = " << this->b_value<<endl;40     cout<<"----------------------------"<<endl;41 }42 43 int main()44 {45     Demo  d1(10);    // 調用建構函式46     d1.printInfo();47 48     Demo d2(d1);     // 調用拷貝建構函式49     d2.printInfo();50 51     Demo d3(0);52     d3 = d1;         // 調用重載賦值操作符53     d3.printInfo();54 55     system("pause");56     return 0;57 }

輸出結果:

object address: 002CFB6Cm_value = 10b_value = 10----------------------------object address: 002CFB5Cm_value = 10b_value = 0----------------------------object address: 002CFB4Cm_value = 10b_value = 0----------------------------請按任意鍵繼續. . .

第四段程式:

 1 #include <iostream> 2 using namespace std; 3  4 class Base{ 5 public: 6     Base():b_value(0){} 7     Base(int value):b_value(value){} 8     Base(const Base& rhs){ 9         this->b_value = rhs.b_value;10     }11     Base& operator=(const Base& rhs){12         this->b_value = rhs.b_value;13         return *this;14     }15     int getValue(){16         return this->b_value;17     }18 19 private:20     int b_value;21 };22 23 class Demo : Base{24 public:25     Demo(int value);                   // 預設建構函式26     Demo(const Demo& rhs);             // 拷貝建構函式27     Demo& operator=(const Demo& rhs);  // 重載賦值運算子28     void printInfo();                  // 列印成員值函數29 private:30     int m_value;31 };32 33 Demo::Demo(int value){34     this->Base::Base(value);35     this->m_value = value;36     // this->b_value = value; no37 }38 39 Demo::Demo(const Demo& rhs):m_value(rhs.m_value){40     // Base::Base(rhs); no41     this->Base::Base(rhs);42 }43 44 Demo& Demo::operator=(const Demo& rhs){45     this->m_value = rhs.m_value;46     // Base::operator=(rhs);  yes47     this->Base::operator=(rhs);48     return *this;49 }50 51 void Demo::printInfo(){52     cout<<"object address: "<<this<<endl;53     cout<<"m_value = "<<this->m_value<<endl;54     cout<<"b_value = " << this->getValue() <<endl;55     cout<<"----------------------------"<<endl;56 }57 58 int main()59 {60     Demo  d1(10);    // 調用建構函式61     d1.printInfo();62 63     Demo d2(d1);     // 調用拷貝建構函式64     d2.printInfo();65 66     Demo d3(0);67     d3 = d1;         // 調用重載賦值操作符68     d3.printInfo();69 70     system("pause");71     return 0;72 }

輸出結果:

object address: 0041FADCm_value = 10b_value = 10----------------------------object address: 0041FACCm_value = 10b_value = 10----------------------------object address: 0041FABCm_value = 10b_value = 10----------------------------請按任意鍵繼續. . .

第五段程式:

 1 #include <iostream> 2 using namespace std; 3  4 class Base{ 5 public: 6     Base():b_value(0){} 7     Base(int value):b_value(value){} 8     Base(const Base& rhs){ 9         init(rhs);10     }11     Base& operator=(const Base& rhs){12         init(rhs);13         return *this;14     }15     int getValue(){16         return this->b_value;17     }18 19 private:20     int b_value;21     void init(const Base& rhs){22         this->b_value = rhs.b_value;23     }24 };25 26 class Demo : Base{27 public:28     Demo(int value);                   // 預設建構函式29     Demo(const Demo& rhs);             // 拷貝建構函式30     Demo& operator=(const Demo& rhs);  // 重載賦值運算子31     void printInfo();                  // 列印成員值函數32 private:33     int m_value;34     void init(const Demo& rhs){35         this->m_value = rhs.m_value;36         this->Base::Base(rhs);37     }38 };39 40 Demo::Demo(int value){41     this->Base::Base(value);42     this->m_value = value;43 }44 45 Demo::Demo(const Demo& rhs){46     init(rhs);47 }48 49 Demo& Demo::operator=(const Demo& rhs){50     init(rhs);51     return *this;52 }53 54 void Demo::printInfo(){55     cout<<"object address: "<<this<<endl;56     cout<<"m_value = "<<this->m_value<<endl;57     cout<<"b_value = " << this->getValue() <<endl;58     cout<<"----------------------------"<<endl;59 }60 61 int main()62 {63     Demo  d1(10);    // 調用建構函式64     d1.printInfo();65 66     Demo d2(d1);     // 調用拷貝建構函式67     d2.printInfo();68 69     Demo d3(0);70     d3 = d1;         // 調用重載賦值操作符71     d3.printInfo();72 73     system("pause");74     return 0;75 }

輸出結果:

object address: 003DFB6Cm_value = 10b_value = 10----------------------------object address: 003DFB5Cm_value = 10b_value = 10----------------------------object address: 003DFB4Cm_value = 10b_value = 10----------------------------請按任意鍵繼續. . .

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.