With the code:
#include <iostream>
class A {};
class B { char x; };
int main()
{
std::cerr << sizeof(A) << " " << sizeof(B) << std::endl;
}
I know that it's a common interview question to ask the size of an empty class - and I know the answer is one.
My question is... what is held in that "1" byte for an empty class (I'm guessing its empty), and what does the compiler do internally to make it so that sizeof B
is the same as sizeof A
in this case?
I'd like to fully understand it rather than just know the answer.
See Question&Answers more detail:os