If you declare virtual functions you should also declare your destructor virtual ;-).
B has a virtual table, because it has a virtual function, namely func0()
. If you declare a function (including a destructor) virtual in a base class, all its derived classes will have the function with same signature virtual as well. And it will cause them to have a vtable. Moreover, B would have the vtable even if you didn't declare func0
in it explicitly.
Non-virtual functions are not referenced through vtables.
See 2.
No. Classes' vtables are constructed based on class declarations. The bodies of class' functions (let alone other functions) are not taken into account. Therefore, B has a vtable, because its function func0()
is virtual.
There also is a tricky detail, although it's not the gist of your question. You declared your function B::func0()
as inline. In gcc
compiler, if a virtual function is declared inline, it retains its slot in virtual table, the slot pointing to a special function emitted for that inline one (that counts as taking its address, which makes the inline emitted). That means, whether the funciton is inline doesn't influence amount of slots in vtable and its necessity for a class.