#include"iostream"
using namespace std;
class Person{
public:
// constructor function
Person(int age)
{
this->p_age = new int (age);
}
// the menber;
int *p_age;
// destructor function
~Person()
{
if(this->p_age)
{
delete this->p_age;
this->p_age=nullptr;
}
}
// Person类的赋值操作设计
Person &operator=(Person &p)
{
// 如果这个类的p_age不是空的话
if(this->p_age)
{
// 就删除这个P_AGE的地址的内存
delete this->p_age;
this->p_age=nullptr;
}
// 这个类的p_age的地址的值就是赋值右边的p_age的地址的值
this->p_age=new int (*p.p_age);
// 返回这个类(this是这个类的地址)
return *this;
}
};
void test1()
{
Person t1(10);
Person t2(20);
Person t3(30);
// 把t3赋值给t2和t1
t1=t2=t3;
// 打印
cout<<*t1.p_age<<endl;
cout<<*t2.p_age<<endl;
cout<<*t3.p_age<<endl;
}
int main(int argc,char *argv[])
{
test1();
return 0;
}