您的位置:首页 > 编程语言 > C语言/C++

C++析构函数的调用

2014-04-07 21:23 375 查看
使用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;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: