[Effective C++读书笔记]0012_复制对象时勿忘其每一部分
第一段程序:
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: 0017FCF4 m_value = 10 m_name = Z ---------------------------- object address: 0017FCE4 m_value = 10 m_name = ---------------------------- object address: 0017FCD4 m_value = 10 m_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: 002CFB6C m_value = 10 b_value = 10 ---------------------------- object address: 002CFB5C m_value = 10 b_value = 0 ---------------------------- object address: 002CFB4C m_value = 10 b_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; no 37 } 38 39 Demo::Demo(const Demo& rhs):m_value(rhs.m_value){ 40 // Base::Base(rhs); no 41 this->Base::Base(rhs); 42 } 43 44 Demo& Demo::operator=(const Demo& rhs){ 45 this->m_value = rhs.m_value; 46 // Base::operator=(rhs); yes 47 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: 0041FADC m_value = 10 b_value = 10 ---------------------------- object address: 0041FACC m_value = 10 b_value = 10 ---------------------------- object address: 0041FABC m_value = 10 b_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: 003DFB6C m_value = 10 b_value = 10 ---------------------------- object address: 003DFB5C m_value = 10 b_value = 10 ---------------------------- object address: 003DFB4C m_value = 10 b_value = 10 ---------------------------- 请按任意键继续. . .