代码拉取完成,页面将自动刷新
#include<iostream>
using namespace std;
class Person
{
public:
Person(int age)
{
m_Age = new int(age);
}
~Person()
{
if (m_Age != NULL)
{
delete m_Age;
m_Age = NULL;
}
}
//赋值运算符重载
Person& operator=(Person& p)
{
if (m_Age != NULL)//应该先判断是否有属性在堆区,如果有,先释放干净,然后深拷贝
{
delete m_Age;
m_Age = NULL;
}
//深拷贝
m_Age = new int(*p.m_Age);
//返回对象本身
return *this;
}
int *m_Age;
};
void test01()
{
Person p1(18);
Person p2(20);
Person p3(25);
p3 = p2 = p1;
cout << "p1的年龄是: " << *p1.m_Age << endl;
cout << "p2的年龄是: " << *p2.m_Age << endl;
cout << "p3的年龄是: " << *p3.m_Age << endl;
}
int main()
{
test01();
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。