也谈析构:析构函数何时被调用

为什么要说“也”?用google搜索“析构函数”是,google会说“约有81,500项符合 析构函数 的查询结果”,我最近复习c++是有所心得,所以“也”想谈谈“析构函数”。我不想像教科书似的介绍它,而是从它何时被调用来浅谈一下。

析构函数在下边3种情况时被调用:
1.对象生命周期结束,被销毁时;
2.delete指向对象的指针时,或delete指向对象的基类类型指针,而其基类虚构函数是虚函数时;
3.对象i是对象o的成员,o的析构函数被调用时,对象i的析构函数也被调用。

情况1请看下边代码:
#include<iostream.h>
class A
{
 public:
 A()
 {
  cout<<"constructing A"<<endl;
 } 
 ~A()
 {
  cout<<"destructing A"<<endl;
 }
 private:
 int a;
};
class B: public A
{
 public:
 B()
 {
  cout<<"constructing B"<<endl;
 }
 ~B()
 {
  cout<<"destructing B"<<endl;
 }
 private:
 int b;
};

void main()
{
 B b;
}

运行结果为:

constructing A
constructing B
destructing B
destructing A

上述代码还说明了一件事:析构函数的调用顺序与构造函数的调用顺序相反。

情况2则正好说明了为什么基类应该把析构函数声明为虚函数,请先看下边的例子:

#include<iostream.h>
class A
{
 public:
 A()
 {
  cout<<"constructing A"<<endl;
 } 
 ~A()
 {
  cout<<"destructing A"<<endl;
 }
 private:
 int a;
};
class B: public A
{
 public:
 B()
 {
  cout<<"constructing B"<<endl;
 }
 ~B()
 {
  cout<<"destructing B"<<endl;
 }
 private:
 int b;
};

void main()
{
 A* a = new B;
 delete a;
}

运行结果为:

constructing A
constructing B
destructing A

若将class A中的析构函数声明为虚函数,运行结果将变成:

constructing A
constructing B
destructing B
destructing A

由此还可以看出虚函数还是多态的基础,才c++中没有虚函数就无法实现多态。因为不声明成虚函数就不能“推迟联编”,所以不能实现多态。这点上和java不同,java总是“推迟联编”的,所以也剩了这些麻烦。

扯远了,再看情况3,通过下边代码表示:
#include<iostream.h>
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;
 
由情况2我们知道,这将不会调用class B的析构函数不会被调用,所以class C的析构函数也不会被调用。
正如我们想的,运行结果为:

constructing A
constructing C
constructing B
destructing A

俗话说温故而知新,我却不想做什么师,只是希望能够和大家分享一下对析构函数和虚析构函数的更深的认识。以上代码在VC++6.0上测试通过,如有疏漏或错误的认识请大家指正:)

  • 22
    点赞
  • 61
    收藏
    觉得还不错? 一键收藏
  • 12
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值