标签:c++
当执行delete c时,析构Test 时(delete p)会出现问题。原因在哪里??
解决方案
#include <iostream>
using namespace std;
class Test
{
public:
Test()
{
}
~Test()
{
cout<<"Test destructor"<<endl;
if(p!=NULL)
delete p;
}
private :
int *p;
};
class Test1
{
public:
Test1()
{
}
~Test1()
{
cout<<"Test1 destructor"<<endl;
}
};
class Test2
{
public:
Test2()
{
}
~Test2()
{
cout<<"Test2 destructor"<<endl;
}
private:
Test a;
Test1 b;
};
int main()
{
Test2 *c=new Test2;
delete c;
//system("pause");
return 0;
}
标签:c++
原文地址:http://blog.csdn.net/greenapple_shan/article/details/45457339