1 //赋值运算符重载 =
 2 #include <iostream>
 3 #include <string>
 4 using namespace std;
 5 
 6 //赋值运算符的重载
 7 class Person
 8 {
 9 public:
10 
11     Person(int age)
12     {
13         m_Age = new int(age);
14     }
15     ~Person()
16     {
17         if (m_Age != NULL)
18         {
19             delete m_Age;
20             m_Age = NULL;
21         }
22     }
23     //重载 赋值运算符
24     Person& operator= (Person& p)
25     {
26         //编译器是提供浅拷贝
27         //m_Age = p.m_Age;
28 
29         //应该先判断是否有属性再堆区,如果有先释放干净 然后再深拷贝
30         if (m_Age != NULL)
31         {
32             delete m_Age;
33             m_Age = NULL;
34         }
35 
36         //深拷贝
37         m_Age = new int(*p.m_Age);
38         //返回对象本身
39 
40         return *this;
41 
42     }
43 
44     int  *m_Age;
45 };
46 
47 void test01()
48 {
49     Person p1(18);
50 
51     Person p2(20);
52 
53     Person p3(30);
54 
55     p1 = p2 = p3;
56 
57     //p2 = p1; //赋值操作
58 
59     cout << *p1.m_Age << endl;
60     cout << *p2.m_Age << endl;
61     cout << *p3.m_Age << endl;
62 }
63 int main()
64 {
65     test01();
66 
67 
68     int a = 10;
69     int b = 20;
70     int c = 30;
71 
72     a = b = c;
73 
74     cout << "a = " << a << endl;
75     cout << "b = " << b << endl;
76     cout << "c = " << c << endl;
77 }

C++ //赋值运算符重载 =_#include