I’ve been reading about this stuff that size of an object should be at least 1 byte (C++: What is the size of an object of an empty class?) and what’s wrong about having two empty objects the same address ? After all, we can have two pointers to the same object.
The googling tells me there is something about object identity fundemantal rule, but I can’t find more detailed info about that.
So… $SUBJ.
Having two objects at the same address would mean that there would be no way to distinguish between these two objects when referencing them with pointers. For example, in the following code:
Should the foo method be called on
o1oro2?It could be argued that since these objects have no data and no virtual methods (otherwise they would have a non-zero size), it doesn’t matter on which instance the method is invoked. However, this becomes more important when we want to test if two objects are equal (i.e. if they are the same):
For a more concrete example, let’s assume that I want to associate some objects (I could say entities) with some values, let’s say in an associative container:
So yes, it all boils down to object identity: if objects were allowed to be empty, they could be deprived of their identity, which is simply not allowed by the language (thankfully).