标签:技术分享 called 详解 stat fir target rtu for virt
C++中的虚函数(表)实现机制以及用C语言对其进行的模拟实现
C++ 多继承和虚继承的内存布局 【已翻译100%】 (虚继承参考,推荐)
VTable Notes on Multiple Inheritance in GCC C++ Compiler v4.0.1
===================================================
1.vptr变量是在构造函数中进行初始化的。又因为要想执行虚函数必须通过vptr变量找到虚函数表。(在构造函数初始化vptr变量之前是不会调用虚函数的)如果可以定义虚构造函数,就陷入了先有鸡还是先有蛋的问题。
2.似乎没有虚构造函数的需求
更多回答请参考 这个链接
执行任何正常的析构函数:
把析构函数写成虚函数的作用,和普通虚函数的作用是一样的。
GCC编译的时候,虚表中会有两个虚析构函数。
The entries for virtual destructors are actually pairs of entries. The first destructor, called the complete object destructor, performs the destruction without calling delete() on the object. The second destructor, called the deleting destructor, calls delete() after destroying the object. Both destroy any virtual bases; a separate, non-virtual function, called the base object destructor, performs destruction of the object but not its virtual base subobjects, and does not call delete().
VS环境下的编译似乎只有一个虚析构函数。
虚继承的实现与具体编译器相关,不同编译器实现有所不同。
class A { public: int a; virtual void v(); }; class B : public virtual A { public: int b; virtual void w(); }; class C : public virtual A { public: int c; virtual void x(); }; class D : public B, public C { public: int d; virtual void y(); }; +-----------------------+ | 20 (vbase_offset) | +-----------------------+ | 0 (top_offset) | +-----------------------+ | ptr to typeinfo for D | +----------> +-----------------------+ d --> +----------+ | | B::w() | | vtable |----+ +-----------------------+ +----------+ | D::y() | | b | +-----------------------+ +----------+ | 12 (vbase_offset) | | vtable |---------+ +-----------------------+ +----------+ | | -8 (top_offset) | | c | | +-----------------------+ +----------+ | | ptr to typeinfo for D | | d | +-----> +-----------------------+ +----------+ | C::x() | | vtable |----+ +-----------------------+ +----------+ | | 0 (vbase_offset) | | a | | +-----------------------+ +----------+ | | -20 (top_offset) | | +-----------------------+ | | ptr to typeinfo for D | +----------> +-----------------------+ | A::v() | +-----------------------+ class A { public: int a; }; class B : public virtual A { public: int b; virtual void w(); }; class C : public virtual A { public: int c; }; class D : public B, public C { public: int d; virtual void y(); }; +-----------------------+ | 20 (vbase_offset) | +-----------------------+ | 0 (top_offset) | +-----------------------+ | ptr to typeinfo for D | +----------> +-----------------------+ d --> +----------+ | | B::w() | | vtable |----+ +-----------------------+ +----------+ | D::y() | | b | +-----------------------+ +----------+ | 12 (vbase_offset) | | vtable |---------+ +-----------------------+ +----------+ | | -8 (top_offset) | | c | | +-----------------------+ +----------+ | | ptr to typeinfo for D | | d | +-----> +-----------------------+ +----------+ | a | +----------+
vbase_offset指的是到基类A的偏移。
标签:技术分享 called 详解 stat fir target rtu for virt
原文地址:https://www.cnblogs.com/dirge/p/9024916.html