纯虚函数位于C++中的哪个位置?

sky*_*oor 8 c++ vtable

哪个虚拟表将是纯虚函数?在基类或派生类中?

例如,每个类中的虚拟表是什么样的?

class Base {

  virtual void f() =0;
  virtual void g();
}


class Derived: public Base{

  virtual void f();
  virtual void g();

}
Run Code Online (Sandbox Code Playgroud)

Art*_*lov 16

g++ -fdump-class-hierarchy layout.cpp生成一个文件layout.cpp.class.内容layout.cpp.class将显示以下内容:

Vtable for Base
Base::_ZTV4Base: 4u entries
0     (int (*)(...))0
8     (int (*)(...))(& _ZTI4Base)
16    __cxa_pure_virtual
24    Base::g

Class Base
   size=8 align=8
   base size=8 base align=8
Base (0x7ff893479af0) 0 nearly-empty
    vptr=((& Base::_ZTV4Base) + 16u)

Vtable for Derived
Derived::_ZTV7Derived: 4u entries
0     (int (*)(...))0
8     (int (*)(...))(& _ZTI7Derived)
16    Derived::f
24    Derived::g

Class Derived
   size=8 align=8
   base size=8 base align=8
Derived (0x7ff893479d90) 0 nearly-empty
    vptr=((& Derived::_ZTV7Derived) + 16u)
  Base (0x7ff893479e00) 0 nearly-empty
      primary-for Derived (0x7ff893479d90)

删除"纯度" f将第五行更改为:

16    Base::f