#include<iostream>
using namespace std;
class Persons
{
public:
int* p_age;
void operator = (Persons& p)
{
p_age = new int(*p.p_age);
}
Persons(){}
Persons(int num)
{
p_age = new int(num);
}
~Persons()
{
delete p_age;
p_age = NULL;
}
};
int main()
{
Persons p1(35);
cout << *p1.p_age << endl;
Persons p2 = p1;
cout << *p2.p_age << endl;
system("pause");
return 0;
}自己写的析构函数为什么是程序在结束时崩了?
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
7条回答 默认 最新
ProfSnail 2021-03-09 22:07关注改成
~Persons()
{
delete []p_age;
}===我怎么感觉你写的也没啥问题呢。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报