I am now testing the result of C++ sizeof
operator:
class S{};
class A:public S{
virtual void fun()=0;
};
class B{};
class C : public B,public A {};
int main(){
cout<<sizeof(A)<<endl;// result is 4
cout<<sizeof(C)<<endl;// result is 8
}
But if I remove the extend of class S:
class A{
virtual void fun()=0;
};
class B{};
class C : public B,public A{};
int main(){
cout<<sizeof(A)<<endl;// result is 4
cout<<sizeof(C)<<endl;// result is 4
}
Can anyone explain the different of the output of sizeof(C)
in those two examples? Class S
is just an empty class (I know, sizeof(S) == 1
), so why does it make a difference whether or not A
has a base class? Especially since it doesn't make any difference to sizeof(A)
, only to sizeof(C)
?