Possible Duplicate:
object size with virtual
Does virtual inheritance change the size of the derived class? I executed the following code, where I have two derived classes one virtually inherited and the other non virtually inherited:
class A {
public:
int a;
virtual void a1();
};
class non_vir_der: public A{
public:
int c;
virtual void aa();
};
class vir_der: public virtual A{
public:
int d;
virtual void bb();
};
int main()
{
cout<<sizeof(non_vir_der)<<"\n";
cout<<sizeof(vir_der)<<"\n";
return 0;
}
output:
12 (imo: 4(int a)+ 4(int c)+ 4(vir ptr))
16 (extra 4?)
to check again if i had missed something, i tried the minimum code required, by deleting all ints in the classes and the output was:
4
4
the second output indicates that the two derived classes are of same size. Why is the size of vir_der 16 in the first run, why is it not 12?