使用new构建的对象,只有当使用delete销毁对象时,才调用析构函数(因为new构造的对象位于堆内存,堆内存要手动释放)
example1
#include <iostream>
using namespace std;
class A
{
public:
A();
~A();
private:
int a;
float b;
};
A::A(void)
{
cout<<"The constructor of the class A"<<endl;
}
A::~A(void)
{
cout<<"The deconstructor of the class A"<<endl;
}
int main(void)
{
{
A a;
}
system("pause");
return 0;
}
example2
#include <iostream>
using namespace std;
class A
{
public:
A();
~A();
private:
int a;
float b;
};
A::A(void)
{
cout<<"The constructor of the class A"<<endl;
}
A::~A(void)
{
cout<<"The deconstructor of the class A"<<endl;
}
int main(void)
{
{
A *a=new A();
}
system("pause");
return 0;
}