-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path20_类与对象_对象特征_05.cpp
60 lines (52 loc) · 1.29 KB
/
20_类与对象_对象特征_05.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
using namespace std;
// 深拷贝与浅拷贝
// 浅拷贝:简单的赋值拷贝操作
// 深拷贝:在堆区重新申请空间,进行拷贝操作
class Person
{
public:
int m_Age;
int *m_Height;
Person()
{
cout << "Person的默认构造函数调用" << endl;
}
Person(int age, int height)
{
m_Age = age;
m_Height = new int(height);
cout << "Person的有参构造函数调用" << endl;
}
// 自己实现拷贝构造函数,解决浅拷贝带来的问题
Person(const Person &p)
{
cout << "Person拷贝构造函数调用" << endl;
m_Age = p.m_Age;
// m_Height = p.m_Height; // 编译器默认实现
// 深拷贝操作
m_Height = new int(*p.m_Height);
}
~Person()
{
// 析构代码,将堆区开辟数据释放操作
if (m_Height != NULL)
{
delete m_Height;
m_Height = NULL;
}
cout << "Person的析构函数调用" << endl;
}
};
void test01()
{
Person p1(18, 170);
cout << "p1年龄为 " << p1.m_Age << " 身高为 " << *p1.m_Height << endl;
Person p2(p1);
cout << "p2年龄为 " << p2.m_Age << " 身高为 " << *p2.m_Height << endl;
}
int main()
{
test01();
return 0;
}