ProgrammingModerateWipro · Capgemini pattern

What does sizeof print for an empty class in C++?

What does sizeof print for an empty class in C++?
class Empty { };
int main() { cout << sizeof(Empty); }
  1. A. 0
  2. B. 8
  3. C. 1
  4. D. 4

Correct answer: C. 1

C++ requires distinct objects to have distinct addresses, so even an empty class occupies at least 1 byte. (As a base class it may take 0 bytes thanks to the empty-base optimisation.)

More OOP with C++ and Java questions

Exams that ask this topic