-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path34_类与对象_运算符重载_04.cpp
66 lines (55 loc) · 1.18 KB
/
34_类与对象_运算符重载_04.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
61
62
63
64
65
66
#include <iostream>
using namespace std;
/*
C++ 编译器至少给一个类添加4个函数:
1.默认构造函数(无参,函数体为空)
2.默认析构函数(无参,函数体为空)
3.默认拷贝构造函数,对属性进行值拷贝
4.赋值运算符 opeartor= ,对属性进行值拷贝
*/
// 赋值运算符重载
class Person
{
public:
int *m_Age;
Person(int age)
{
m_Age = new int(age);
}
~Person()
{
if (m_Age != NULL)
{
delete m_Age;
m_Age = NULL;
}
}
// 重载赋值运算符
Person &operator=(Person &p)
{
// 编译器提供浅拷贝,类似 m_Age = p.m_Age;
// 应该先判断是否有属性在堆区,如有先释放干净,然后再深拷贝
if (m_Age != NULL)
{
delete m_Age;
m_Age = NULL;
}
m_Age = new int(*p.m_Age);
return *this;
}
};
void test01()
{
Person p1(18);
Person p2(20);
Person p3(30);
p3 = p2 = p1; // 赋值操作
cout << *p1.m_Age << endl;
cout << *p2.m_Age << endl;
cout << *p3.m_Age << endl;
}
int main()
{
test01();
return 0;
}