環境
g++ --version
g++ (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
命令:
g++ --std=c++11 -fdump-class-hierarchy main.cpp
代碼和結果
class NullClassObj {
};
class InfoClass {
private:
int a;
};
class Base {
public:
Base() {}
virtual ~Base() {}
virtual void VFunc() {}
virtual void VFunc2() {}
private:
int a;
};
class Derived : public Base {
public:
Derived() {}
~Derived() override {}
void VFunc() override {}
private:
int b;
};
int main()
{
Base *ptr = new Derived();
delete ptr;
return 0;
}
執行g++ --std=c++11 -fdump-class-hierarchy main.cpp,生成如下結果:
Class NullClassObj
size=1 align=1
base size=0 base align=1
NullClassObj (0x0x7f2ca7ad0960) 0 empty
Class InfoClass
size=4 align=4
base size=4 base align=4
InfoClass (0x0x7f2ca7ad09c0) 0
Vtable for Base
Base::_ZTV4Base: 6 entries
0 (int (*)(...))0
8 (int (*)(...))(& _ZTI4Base)
16 (int (*)(...))Base::~Base
24 (int (*)(...))Base::~Base
32 (int (*)(...))Base::VFunc
40 (int (*)(...))Base::VFunc2
Class Base
size=16 align=8
base size=12 base align=8
Base (0x0x7f2ca7ad0a20) 0
vptr=((& Base::_ZTV4Base) + 16)
Vtable for Derived
Derived::_ZTV7Derived: 6 entries
0 (int (*)(...))0
8 (int (*)(...))(& _ZTI7Derived)
16 (int (*)(...))Derived::~Derived
24 (int (*)(...))Derived::~Derived
32 (int (*)(...))Derived::VFunc
40 (int (*)(...))Base::VFunc2
Class Derived
size=16 align=8
base size=16 base align=8
Derived (0x0x7f2ca797d1a0) 0
vptr=((& Derived::_ZTV7Derived) + 16)
Base (0x0x7f2ca7ad0ea0) 0
primary-for Derived (0x0x7f2ca797d1a0)
結果說明
// TODO