I saw this question. this post is so old. so i got different output.
test code
#include <iostream>
class M{
char k[ 3 ];
public:
void m(){};
};
class A{
char k[ 3 ];
public:
virtual void a(){};
};
class B : public A{
char j[ 3 ];
public:
virtual void b(){};
};
class C : public virtual A{
char i[ 3 ];
public:
virtual void c(){};
};
class D : public B, public C{
char h[ 3 ];
public:
virtual void d(){};
};
int main(){
A a;
B b;
C c;
D d;
std::cout << sizeof(M) << std::endl;
std::cout << sizeof(a) << std::endl;
std::cout << sizeof(b) << std::endl;
std::cout << sizeof(c) << std::endl;
std::cout << sizeof(d) << std::endl;
}
and output
3
16
16
32
48
I understand why B have same size as A with this post. But still i dont get it why C's size is 32.
C has subobject of A(16) + new array(3) + pointer to A(8) and maybe padding(5).
It makes sense if B didn't reuse the padding, but it doesn't make sense because I thought B was the same size as A because he reused it.
I don't understand that result. can you help me to get it?!
I search some other post..