#include <iostream>
using namespace std;
class Human
{
public:
int get()const{return *i;}
void set(int x){*i=x;}
~Human();
Human();
private:
int *i;
};
int main()
{
Human *p=new Human;
p->set(1); //如果屏蔽这一句,结果为“构造函数执行中··· 999 析构函数执行中···”
//如果不屏蔽这一句,结果为“构造函数执行中··· 1 析构函数执行中···”
cout<<p->get()<<endl;
delete p;
return 0;
}
Human::Human()
{
cout<<"构造函数执行中...\n";
i=new int(999);
}
Human::~Human()
{
cout<<"析构造函数执行中...\n";
delete i;
}
using namespace std;
class Human
{
public:
int get()const{return *i;}
void set(int x){*i=x;}
~Human();
Human();
private:
int *i;
};
int main()
{
Human *p=new Human;
p->set(1); //如果屏蔽这一句,结果为“构造函数执行中··· 999 析构函数执行中···”
//如果不屏蔽这一句,结果为“构造函数执行中··· 1 析构函数执行中···”
cout<<p->get()<<endl;
delete p;
return 0;
}
Human::Human()
{
cout<<"构造函数执行中...\n";
i=new int(999);
}
Human::~Human()
{
cout<<"析构造函数执行中...\n";
delete i;
}