Firstly this is a C++ issue, it is strictly against the rules of C to create a class with no members. This makes sense because the only real use for a structure or class with no data members and virtual functions is as the base from which to derive other classes and structures or as a container for non-virtual methods.
The reason this happens boils down to properly implementing the standard, one of the things the C++ standard says is that "no object shall have the same address in memory as any other variable". There is a very good reason for this, take an array of type T then subtracting the pointer to 1 entry in the array from another should give the difference in indexes
Expand|Select|Wrap|Line Numbers
- T array[5];
- int diff = &array[3] - &array[2];
- // diff = 1
diff = ((char *)&array[3] - (char *)&array[2]) / sizeof T;
This is just an example, but this type of calculation reliant on the sizeof T occurs in all sorts of pointer arithmetic.
If objects were allowed to have the same address because their type was 0 sized then this calculation would not be possible because
Expand|Select|Wrap|Line Numbers
- &array[3] - &array[2] = &array[3] - &array[1]
- = &array[3] - &array[1]
- = &array[3] - &array[0]
- = 0
Additionally there is the problem that the compiler had to divided by sizeof T, which would be a divide by zero error, although this could be coded round.
Allowing objects to have the same memory address would result in the need to implement some rather complex code to handle pointer arithmetic on them.
So objects must not have the same memory address, that is they must be individually addressable. What is the easiest way to ensure this? Make sure that all types have a non-zero size. In order to achieve this the compiler adds a dummy byte to structures and classes that have no data members and no virtual functions so that they have a size of 1 rather than a size of 0 and then they are guaranteed to have a unique memory address.