Apr 26, 2014

[C++] struct / class not only different in access level, but in size through inheritance , because of POD

Output of the following code:
4,4,6  2,2,2

POD or non-POD affect the layout size.
 
#include <iostream>

struct S {
   short a;
   char b;
};

struct ES : S {
   char c;
};

class C {
   short a;
   char b;
};

class EC : C {
   char c;
};



int main() {
   std::cout << sizeof(C) << std::endl;
   std::cout << sizeof(EC) << std::endl;
   std::cout << sizeof(ES) << std::endl;
   std::cout << "alignof(C) " << alignof(C) << std::endl;
   std::cout << "alignof(EC) " << alignof(EC) << std::endl;
   std::cout << "alignof(ES) " << alignof(ES) << std::endl;


   return 0;
}
Reference:
The Lost Art of C Structure Packing by Eric S. Raymond
Access specifiers (public/private/protected) don't affect inherited "object size" in anyway.?? not quite

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.