Try to compile and run this program with your C compiler and with your C++ compiler:
#include <stdio.h>
struct empty {};
int main()
{
printf("nothing is %d\n", sizeof(struct empty));
}
How big should struct empty, which contains
nothing, be?
C gets this right: 0 bytes. But C++ requires it to take 1 byte. That's so that various object identity weirdnesses work out correctly in more complicated cases. But for struct empty it's just plain crazy...
As Gorgonzola points out, it gets crazier than this. Suppose I had to write a class that implements 10 interfaces. Each interface is an empty base class, so it has size 1. Does it follow that the derived class needs at least 10 bytes, just to store 10 copies of nothing?
No. The empty base-class optimization is to remove all storage for empty base classes. This is required for any reasonable quality of implementation, but also adds to the craziness. It is commonly assumed that if I declare
class C: public A, protected B { ... };
then sizeof(C) >= sizeof(A)+sizeof(B). Except, of course, that if A, B and C are all empty, this may quite simply fail!
In C++, derived classes can be smaller than the sum of their base classes. But, for obvious reasons, a class cannot be smaller than the sum of its contents -- even if its contents are all empty.