注册 登录
编程论坛 C++论坛

【求助】【C++析构函数错误分析】

食来运转 发布于 2016-09-15 15:18, 1208 次点击
程序代码:
问题:析构函数为何不被执行?


程序代码如下:

#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]


谢谢~

0 回复
1