Friday, 7 March 2014

Virtual table

How to see the virtual table of inheritance hierarchy? Use -fdump-class-hierarchy option with g++.

vtable.cxx:
class Base {
public:
    virtual void foo() = 0;
    virtual void bar() {}
};

class Derived : public Base {
public:
    void foo() {}
    void bar() {}
};

$ g++ -fdump-class-hierarchy vtabl.cxx

File vtable.cxx.002t.class generated as:
Vtable for Base
Base::_ZTV4Base: 4u entries
0     (int (*)(...))0
8     (int (*)(...))(& _ZTI4Base)
16    (int (*)(...))__cxa_pure_virtual
24    (int (*)(...))Base::bar

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

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

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

No comments:

Post a Comment