I think sizeof(Base) should be 12. Why is it 16?
Without the virtual function, I get 4 and 8.
class Base{
public:
int i;
virtual void Print(){cout<<"Base Print";}
};
class Derived:public Base{
public:
int n;
virtual void Print(){cout<<"Derived Print";}
};
int main(){
Derived d;
cout<<sizeof(Base)<<","<<sizeof(d);
return 0;
}
expected result:12,16
actual result:16,16
This happens due to compiler deciding to align your classes.
If you want (or need) structs or classes to have their "real" sizes, you can use
#pragma pack(1)like this: