4级继承链中的C ++虚拟析构函数. [英] C++ Virtual Destructors in a 4 level inheritance chain.

查看:110
本文介绍了4级继承链中的C ++虚拟析构函数.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对虚拟析构函数进行一些小实验以进行审查-想知道是否有人对以下内容有简单的解释(使用vs 2010):

I was doing a little experiment with virtual destructors to review - wondering if anyone has a simple explanation for the following (using vs 2010):

我定义类层次结构A-B-C-D,D继承C,C继承B,B继承A,A是基数;

I Define class hierarchy A-B-C-D, D inherits C, C inherits B, B inherits A, A is the Base;

进行了2个实验:

第一个实验-

A有一个虚拟的析构函数.

A has a virtual Destructor.

B具有非虚拟析构函数

B has a non-Virtual Destructor

C有一个虚拟的析构函数

C has a virtual Destructor

D具有非虚拟的析构函数

D has a non virtual Destructor

//----------------------------

//----------------------------

在类型D的堆上分配4个对象-将A *,B *和C *的指针指向前3个-将第4个对象作为D *保留完整性. 删除所有4个指针.

Allocate 4 objects on the heap of type D - Point a pointer of A*, B* and C* at the first 3 - Leave the 4th as a D* for Completeness. Delete all 4 Pointers.

如我所料,在所有4种情况下,完整的析构函数链都以相反的顺序从D向下执行到A,从而释放了所有内存.

As I expected, in all 4 instances, the complete destructor chain is executed in reverse order from D down to A, freeing all memory.

第二个实验-

A具有非虚拟的析构函数,**将A更改为非虚拟的

A has a non-virtual Destructor ** Changed A to non virtual

B具有非虚拟析构函数

B has a non-Virtual Destructor

C有一个虚拟的析构函数

C has a virtual Destructor

D具有非虚拟Distructor

D has a non virtual Distructor

在类型D的堆上分配4个对象-将A *,B *和C *的指针指向前3个-将第4个对象作为D *保留完整性.

Allocate 4 objects on the heap of type D - Point a pointer of A*, B*, and C* at the first 3 - Leave the 4th as a D* for Completeness.

删除C *和D *指针: 完整的析构函数链以从D到A的相反顺序执行,从而释放了所有内存.

Deleting C* and D* pointers: the complete destructor chain is executed in reverse order from D down to A, freeing all memory.

删除B *: B然后运行A析构函数(泄漏)

Deleting B*: B and then A Destructor is run (leak)

删除A *: 仅运行析构函数(泄漏)

Deleting A*: Only A Destructor is run (leak)

任何人都可以解释这是为什么吗?

Can anyone explain Why this is?

在实验2中分配了D类对象时,其直接基类(C)具有虚拟析构函数-这不是告诉编译器使用Vptr对其进行跟踪并知道内存类型吗?不管参考资料吗?

When D type opjects are allocated in experiment 2, its immediate base class (C) has a virtual destructor - doesnt that tell the compiler to track it with a Vptr and know the memory type? REGARDLESS of the reference?

谢谢 迈克

推荐答案

在实验2中分配了D类对象时,其直接基类(C)具有虚拟析构函数-这不是告诉编译器使用Vptr对其进行跟踪并知道内存类型吗?不管参考资料吗?

When D type opjects are allocated in experiment 2, its immediate base class (C) has a virtual destructor - doesnt that tell the compiler to track it with a Vptr and know the memory type? REGARDLESS of the reference?

否.

在第二个测试用例中,AB没有vptrs/vtables. (即使它们确实做到了,非虚拟成员函数仍将静态地解决,而不是动态地解决.)

In your second test case, A and B don't have vptrs/vtables. (And even if they did, a non-virtual member function would still be resolved statically, not dynamically.)

换一种说法,基类不继承"派生类的信息(例如,函数是否是虚拟的).

Put another way, a base class does not "inherit" information (such as whether functions are virtual) from derived classes.

这篇关于4级继承链中的C ++虚拟析构函数.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆