Here is my code:
#include <iostream>
using namespace std;
class MyClass{
int mem1;
float mem2;
};
class MyKids: public virtual MyClass{
int cmem1;
int cmem2;
};
class MyLawKids:public virtual MyClass{
int lmem1;
};
class MyGrands:public MyKids, public MyLawKids{
int gmem1;
};
int main(){
cout << "\n Size of MyClass: " << sizeof(MyClass) << " bytes" << endl ;
cout << " Size of MyKids: " << sizeof(MyKids) << " bytes" << endl ;
cout << " Size of MyLawKids: " << sizeof(MyLawKids) << " bytes" << endl ;
cout << " Size of MyGrands: " << sizeof(MyGrands) << " bytes" << endl ;
return 0;
}
Result:
Size of MyClass: 8 bytes
Size of MyKids: 20 bytes
Size of MyLawKids: 16 bytes
Size of MyGrands: 32 bytes
Process returned 0 (0x0) execution time : 0.094 s
Press any key to continue.
I think the size of MyGrands class its should be 36 bytes = (sizeof(MyKids + MyLawKids + int gmem1) - 4 bytes of virtual table pointer).
Why my result shown me "Size of MyGrands: 32 bytes" ?