Consider the following code:
#include <iostream>
In my system, it prints
4 8 12 0 0 0
This means that none of these classes is polymorphic. However, the sizes of B1 and B2 differ exactly in the size of the pointer, which is probably a pointer to a vtable. I executed gcc with -fdump-class-hierarchy and got:
Vtable for B2 B2::_ZTV2B2: 3u entries 0 4u 4 (int (*)(...))0 8 (int (*)(...))(& _ZTI2B2) VTT for B2 B2::_ZTT2B2: 1u entries 0 ((& B2::_ZTV2B2) + 12u) Class B2 size=8 align=4 base size=4 base align=4 B2 (0x0x3ca5400) 0 nearly-empty vptridx=0u vptr=((& B2::_ZTV2B2) + 12u) A (0x0x3c972d8) 4 virtual vbaseoffset=-12
The question is: what is a polymorphic class? "Does vtable" mean "be polymorphic and have RTTI" and vice versa? If not, then why would he have at least one virtual function to be polymorphic, if in any case it will have a vtable?
It seems that the definition of a polymorphic class is different from a "class with vtable" because, as we see above, B2 is not polymorphic, but has a vtable and, as I think, should have RTTI. This document clearly states that "polymorphic" - i.e. have at least one virtual function (it is necessary for RTTI to use allowed code on classes.) "
source share