#include<cstdio> classFruit{ public: virtualvoidprintName(){ puts("I am a fruit."); } virtualvoidfoo(){ puts("This function is only for base classes!"); } }; classApple: public Fruit{ public: virtualvoidprintName(){ puts("I am an apple."); } }; intmain(){ Fruit* a = new Apple; a->printName(); //output:I am an apple. a->foo(); //output:This function is for base classes! return0; }
Vtable for Fruit Fruit::_ZTV5Fruit: 4 entries 0 (int (*)(...))0 8 (int (*)(...))(& _ZTI5Fruit) 16 (int (*)(...))Fruit::printName 24 (int (*)(...))Fruit::foo
Class Fruit size=8 align=8 base size=8 base align=8 Fruit (0x0x7f97c22e3d20) 0 nearly-empty vptr=((& Fruit::_ZTV5Fruit) + 16)
Vtable for Apple Apple::_ZTV5Apple: 4 entries 0 (int (*)(...))0 8 (int (*)(...))(& _ZTI5Apple) 16 (int (*)(...))Apple::printName 24 (int (*)(...))Fruit::foo
Class Apple size=8 align=8 base size=8 base align=8 Apple (0x0x7f97c2188270) 0 nearly-empty vptr=((& Apple::_ZTV5Apple) + 16) Fruit (0x0x7f97c22e3f00) 0 nearly-empty primary-for Apple (0x0x7f97c2188270)