java怎么调用析构函数_析构函数何时被调用

析构函数在对象生命周期结束、主动删除或作为成员对象时被调用。当对象生命周期结束或离开作用域,非静态成员的析构函数会被调用。如果对象通过new创建,需要手动delete来调用析构函数,否则可能导致内存泄漏。如果基类析构函数为虚函数,删除派生类指针时会调用所有析构函数。
摘要由CSDN通过智能技术生成

析构函数何时被调用

析构函数在下边3种情况时被调用:

对象生命周期结束,被销毁时;

主动调用delete ;

对象i是对象o的成员,o的析构函数被调用时,对象i的析构函数也被调用。

第一种情况

#include

using namespace std;

class A

{

public:

A()

{

cout << "constructing A" << endl;

}

~A()

{

cout << "destructing A" << endl;

}

private:

int a;

};

void main()

{

A a;

}

运行结果:

constructing A

destructing A

1

2

第二种情况

如果是new的对象,即使离开了作用域也会一直存在,必须主动delete,否则只有在结束程序时才会执行析构。这里在说下内存泄漏,举个例子

#include

using namespace std;

class A

{

public:

A()

{

cout << "constructing A" << endl;

}

~A()

{

cout << "destructing A" << endl;

}

private:

int a;

};

void fun()

{

A *a = new A();

}

int main()

{

while (1)

{

fun();

}

return 0;

}

当离开fun时,虽然离开了作用域,但用new动态开辟空间的对象是不会析构的,你可以观察任务管理器,看到内存一直在上升。但你在其他地方确无法使用a所开辟的空间,因为a这个指针是保存在栈上的,当离开作用域后就自动析构(或者说自动消失了),但它所在分配空间是分配在堆上的,只有主动析构或程序结束,才会释放空间,也就是丢失了这块空间的地址,无法操作这块空间了 。

第三种情况

#include

using namespace std;

class A

{

public:

A()

{

cout << "constructing A" << endl;

}

~A()

{

cout << "destructing A" << endl;

}

private:

int a;

};

class C

{

public:

C()

{

cout << "constructing C" << endl;

}

~C()

{

cout << "destructing C" << endl;

}

private:

int c;

};

class B : public A

{

public:

B()

{

cout << "constructing B" << endl;

}

~B()

{

cout << "destructing B" << endl;

}

private:

int b;

C c;

};

void main()

{

B b;

}

运行结果:

constructing A

constructing C

constructing B

destructing B

destructing C

destructing A

B的析构函数调用之后,又调用了B的成员c的析构函数 。

若将上边的代码中的main()函数内容改成

A* a = new B;

delete a;

我们知道,这将不会调用class B的析构函数不会被调用,所以class C的析构函数也不会被调用。

运行结果:

constructing A

constructing C

constructing B

destructing A

若将class A中的析构函数声明为虚函数 ,这时class B的析构函数也会被调用,例如:

#include

using namespace std;

class A

{

public:

A()

{

cout << "constructing A" << endl;

}

virtual ~A()

{

cout << "destructing A" << endl;

}

private:

int a;

};

class C

{

public:

C()

{

cout << "constructing C" << endl;

}

~C()

{

cout << "destructing C" << endl;

}

private:

int c;

};

class B : public A

{

public:

B()

{

cout << "constructing B" << endl;

}

~B()

{

cout << "destructing B" << endl;

}

private:

int b;

C c;

};

void main()

{

A* a = new B;

delete a;

}

运行结果:

constructing A

constructing C

constructing B

destructing B

destructing C

destructing A

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值