While answering one of the question, there was a discussion thread below my answer. Which suggests that depending on the access specifier (or may be the type of inheritance) private/protected/public the sizeof the class object may vary!
I still don’t understand from their brief discussion, how is that possible ?
Note new language for C++11 below
In C++03, there is language that makes this possible, 9.2 [class.mem]/12 (emphasis mine):
So given this definition:
on a system with 32 bit (
int) alignment, the compiler is not allowed to reordercto come beforeb, forcing the insertion of additional padding padding in betweenaandb, and aftercto the end of the object (makingsizeof(Foo) == 12). However, for this:aand (bandc) are separated by an access specifier, so the compiler is free to perform such reordering, makingsizeof(Foo) == 8.In C++11, the language changes slightly. N3485 9.2 [class.mem]/13 says (emphasis mine):
This means that in C++11, in the above example (separated by 3 publics), the compiler is still not allowed to perform the reordering. It would have to be something like
, which places
a,b, andcwith different access control.Note that under the C++11 rules, given a definition like:
the compiler must put
dafterb, even though they are separated by access specifiers.(That said, I’m not aware of any implementation that actually takes advantage of the latitude offered by either standard)