博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
浅拷贝和深拷贝
阅读量:4695 次
发布时间:2019-06-09

本文共 955 字,大约阅读时间需要 3 分钟。

1 #include
2 using namespace std; 3 4 class person 5 { 6 public: 7 8 person(int age,int height) 9 {10 m_age = age;11 m_Height = new int(height);//我们在堆区开辟了内存,我们需要在某个时候释放 12 puts("有参构造函数调用");13 }14 15 person(const person &p)16 {17 puts("拷贝构造函数调用");18 m_age = p.m_age;19 m_Height = new int(*p.m_Height);20 }21 ~person()22 {23 if(m_Height != NULL)24 {25 delete m_Height;26 m_Height = NULL;27 } 28 puts("析构函数调用");29 }30 31 int m_age;32 int *m_Height;33 };34 35 36 void test01()37 {38 person p(22,160);39 cout << p.m_age << " " << *p.m_Height << endl;40 person p2(p);41 cout << p2.m_age << " " << *p2.m_Height << endl;42 } 43 44 int main()45 {46 //test();47 test01();48 return 0;49 }
View Code

 

分别是浅拷贝和深拷贝的示意图

 

转载于:https://www.cnblogs.com/mch5201314/p/11602852.html

你可能感兴趣的文章
Git Stash用法
查看>>
sql server 2008学习8 sql server存储和索引结构
查看>>
Jquery radio选中
查看>>
postgressql数据库中limit offset使用
查看>>
测试思想-集成测试 关于接口测试 Part 2
查看>>
windows下mysql密码忘了怎么办?【转】
查看>>
php生成器使用总结
查看>>
T-SQL中的indexof函数
查看>>
javascript基础之数组(Array)对象
查看>>
mysql DML DDL DCL
查看>>
RAMPS1.4 3d打印控制板接线与测试1
查看>>
python with语句中的变量有作用域吗?
查看>>
24@Servlet_day03
查看>>
初级ant的学习
查看>>
memcached 细究(三)
查看>>
RSA System.Security.Cryptography.CryptographicException
查看>>
webservice整合spring cxf
查看>>
[解题报告] 100 - The 3n + 1 problem
查看>>
Entity Framework 学习高级篇1—改善EF代码的方法(上)
查看>>
Mybatis逆向工程配置文件详细介绍(转)
查看>>