当前位置: 首页 > news >正文

[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
----------------------------
请按任意键继续. . .

 

 

 

转载于:https://www.cnblogs.com/alephsoul-alephsoul/archive/2012/12/27/2834971.html

相关文章:

  • 返回顶部的js实现
  • 将新添加硬盘划到根目录的方法(利用lvm)
  • DebuggerStepThroughAttribute 类
  • poj1450
  • 惠普安腾服务器打“融合”牌
  • Android 搜索框自动提示及其保存历史记录
  • 无法运行宏,可能是因为该宏在此工作簿中不可用,或者所有的宏都被禁用的解决方法...
  • 通过“时间管理",加快学习进度
  • 美女电子相册
  • CMAKE2.8.10在HP-UNIX上安装异常解决
  • Reflow
  • emacs-shell
  • WPF 去除头部,实现拖动
  • shell四大变量和实例_李强强
  • 通过SharpShell快速实现Windows Shell扩展
  • Java Agent 学习笔记
  • Leetcode 27 Remove Element
  • Node项目之评分系统(二)- 数据库设计
  • python学习笔记-类对象的信息
  • Quartz实现数据同步 | 从0开始构建SpringCloud微服务(3)
  • React+TypeScript入门
  • 从@property说起(二)当我们写下@property (nonatomic, weak) id obj时,我们究竟写了什么...
  • 道格拉斯-普克 抽稀算法 附javascript实现
  • 蓝海存储开关机注意事项总结
  • 每天10道Java面试题,跟我走,offer有!
  • 如何学习JavaEE,项目又该如何做?
  • 云大使推广中的常见热门问题
  • !!java web学习笔记(一到五)
  • (bean配置类的注解开发)学习Spring的第十三天
  • (C#)一个最简单的链表类
  • (vue)el-checkbox 实现展示区分 label 和 value(展示值与选中获取值需不同)
  • (差分)胡桃爱原石
  • (二)换源+apt-get基础配置+搜狗拼音
  • (附源码)springboot工单管理系统 毕业设计 964158
  • (附源码)springboot炼糖厂地磅全自动控制系统 毕业设计 341357
  • (附源码)基于SpringBoot和Vue的厨到家服务平台的设计与实现 毕业设计 063133
  • (附源码)计算机毕业设计ssm电影分享网站
  • (离散数学)逻辑连接词
  • (转)清华学霸演讲稿:永远不要说你已经尽力了
  • .gitignore文件设置了忽略但不生效
  • .Net 4.0并行库实用性演练
  • .NET Core 版本不支持的问题
  • .NET Micro Framework初体验
  • .net 简单实现MD5
  • .net 开发怎么实现前后端分离_前后端分离:分离式开发和一体式发布
  • .NET 中 GetProcess 相关方法的性能
  • .net6解除文件上传限制。Multipart body length limit 16384 exceeded
  • .NET6实现破解Modbus poll点表配置文件
  • .NET成年了,然后呢?
  • .Net中ListT 泛型转成DataTable、DataSet
  • @Bean, @Component, @Configuration简析
  • @SuppressLint(NewApi)和@TargetApi()的区别
  • @Transactional 详解
  • [Android]How to use FFmpeg to decode Android f...
  • [C puzzle book] types