I was reading this post and the end of discussion "vlad from moscow" said that an empty class can have any size. When I performed it practically, I found that the size of an object of empty class is always 1 byte. What " vlad from moscow" said and this one byte size are puzzling me. Cab anyone explain?
> "vlad from moscow" said that an empty class can have any size.
A base class sub-object may have a size of zero.
Every other object (other than a bit-field) occupies some integral number of bytes - ie. has a size of 1, 2, 3 ... N.
1 2 3 4 5
struct A {} ; // empty
A a1, a2 ; // a1 and a2 must have different, distinct addresses.
A array[5] ; // each element of the array must have a unique address of its own
sizeof(A) is typically one, but in theory, it may be more.
In theory, there is no difference between theory and practice. In practice, there is. - unknown
(often attributed to Yogi Berra)
However, if the base class is empty, the C++ standard permits an implementation to apply Empty Base Class Optimization (EBCO) aka Empty Base Optimization (EBO).
The details: http://en.cppreference.com/w/cpp/language/ebo