Given the code:
class A{};
class B : public virtual A{};
class C : public virtual A{};
class D : public B,public C{};
int main(){
cout<<"sizeof(D)"<<sizeof(D);
return 0;
}
Output:
sizeof(D) 8
Every class contains its own virtual pointer only not of any of its base class,
So, why the Size of class(D) is 8?
It depends on compiler implementation. My compiler is Visual Stdio C++ 2005.
Code like this:
It will output
class B has only one virtual pointer. So
sizeof(B)=4. And class C is also.But D multiple inheritance the
class Bandclass C. The compile don’t merge the two virtual table.Soclass Dhas two virtual pointer point to each virtual table.If D only inheritance one class and not virtual inheritance. It will merge they virtual table.