In C++, why does a derived class that just contains a union with an instance of its base class take more memory than the size of the union?
问题 More specifically, a class, inheriting from an empty class, containing just a union whose members include an instance of the base data-less class, takes up more memory than just the union. Why does this happen and is there any way to avoid spending the extra memory? The following code illustrates my question: #include <iostream> class empty_class { }; struct big : public empty_class { union { int data[3]; empty_class a; }; }; struct small { union { int data[3]; empty_class a; }; }; int main()