hi, anyone can help me. i have below codes
Code:
class A
{
char k[3];
public:
virtual void aa() {}
};
class B : public virtual A
{
char j[3];
public:
virtual void bb() {}
};
class C : public virtual B
{
char i[3];
public:
virtual void cc() {}
};
class D : public B, public C
{
char l[3];
virtual void dd() {}
};
int main (int argc, char *argv[]) {
cout << "A: " << sizeof(A) << endl
<< "B: " << sizeof(B) << endl
<< "C: " << sizeof(C) << endl
<< "D: " << sizeof(D) << endl;
return 0;
}
the output is
A: 8
B: 16
C: 24
D: 36
I don't why , especially the last one.
I know The A is 8, because there is a pointer point to the virtual function table as well.
In B and C, if there are no virtual, the output would be B: 12, C: 16 and D would be 28.
so can anybody explain to me why this happens, what's the principle.
thanks for your help!