
问题:析构函数为何不被执行?
程序代码如下:
#include "iostream"
#include "string.h"
using namespace std;
class person
{
private:
char num[20];
char *name;
int age;
public:
person(char s1[],char *p,int a)
{
strcpy(num,s1);
name=new char[strlen(p)+1];
strcpy(name,p);
age=a;
cout<<"构造函数被执行!\n";
}
~person();
void show();
};
person::~person()
{
if(name) delete []name;
cout<<"析构函数被执行!\n";
}
void person::show()
{
cout<<"身份证号:"<<num<<endl;
cout<<" 姓名:"<<name<<endl;
cout<<" 年龄:"<<age<<endl;
}
void main()
{
person p("441424199612076994","张望",28);
p.show();
system("pause");
}
[local]1[/local]
谢谢~
程序代码如下:
#include "iostream"
#include "string.h"
using namespace std;
class person
{
private:
char num[20];
char *name;
int age;
public:
person(char s1[],char *p,int a)
{
strcpy(num,s1);
name=new char[strlen(p)+1];
strcpy(name,p);
age=a;
cout<<"构造函数被执行!\n";
}
~person();
void show();
};
person::~person()
{
if(name) delete []name;
cout<<"析构函数被执行!\n";
}
void person::show()
{
cout<<"身份证号:"<<num<<endl;
cout<<" 姓名:"<<name<<endl;
cout<<" 年龄:"<<age<<endl;
}
void main()
{
person p("441424199612076994","张望",28);
p.show();
system("pause");
}
[local]1[/local]
谢谢~