This is of course implementation-dependent. And it would make a terrible interview question. A good C++ programmer can just trust sizeof
to be right and let the compiler worry about those vtable things.
But what’s going on here is that a typical vtable-based implementation needs two vtables in objects of class C
or D
. Each base class needs its own vtable. The new virtual methods added by C
and D
can be handled by extending the vtable format from one base class, but the vtables used by A
and B
can’t be combined.
In pseudo-C-code, here’s how a most derived object of type D looks on my implementation (g++ 4.4.5 Linux x86):
void* D_vtable_part1[] = { (void*) 0, &D_typeinfo, &A::f1, &C::f3, &D::f4 };
void* D_vtable_part2[] = { (void*) -4, &D_typeinfo, &B::f2 };
struct D {
void** vtable_A;
void** vtable_B;
};
D d = { D_vtable_part1 + 1, D_vtable_part2 + 1 };