In C++, an empty class is defined as a class that does not declare any members (data or functions). Although it seems simple, empty classes have special properties and behaviors in C++ that are worth exploring.
What is an Empty Class?
An empty class is a class that does not contain any user-defined members, for example:
// Definition of an empty classclass EmptyClass { // No member declarations};
Despite appearing “empty”, the C++ compiler automatically generates some default member functions for it, which means that the empty class is not actually “empty”.
Default Member Functions of an Empty Class
When we define an empty class, the compiler automatically generates the following 6 member functions:
- Default constructor
- Default destructor
- Copy constructor
- Copy assignment operator
- Move constructor (C++11 and later)
- Move assignment operator (C++11 and later)
All these functions are public and inline. This can be verified through code:
Program output:
&obj1: 0000000BBF58FA74
&obj4: 0000000BBF58FAD4
&obj2: 0000000BBF58FA94
&obj4: 0000000BBF58FAD4
Size of an Empty Class
A seemingly strange phenomenon is that the size of an empty class in C++ is not 0, but rather 1 byte:
cout << "Size of empty class: " << sizeof(EmptyClass) << endl;
// Output 1
Explanation of the Reason:
- This is to ensure that different objects of the class have different memory addresses
- If the size of the empty class were 0, all objects of that class would share the same address
- The 1-byte size does not store any actual data, it is merely a placeholder to distinguish different objects
Inheritance of Empty Classes
An empty class can be inherited from, and it can also inherit other classes:
// Empty class as a base classclass Base {};class Derived : public Base {};// Empty class as a derived classclass EmptyDerived : public Base {};
When inheriting from an empty class, the C++ compiler typically performsempty base class optimization, which ensures that the derived class does not incur additional size due to inheriting from an empty base class:

Considerations
- Once a user-defined constructor is defined, the compiler will no longer generate the default constructor
- Similarly, defining a copy constructor will prevent the compiler from generating the default version
- In C++11, you can use
<span>= default</span>to explicitly request the compiler to generate the default version:
class EmptyClass {public: EmptyClass() = default; EmptyClass(const EmptyClass&) = default; };
- Using
<span>= delete</span>can prohibit the generation of specific default functions:
class NonCopyable {public: NonCopyable() = default; NonCopyable(const NonCopyable&) = delete; NonCopyable& operator=(const NonCopyable&) = delete; };
Conclusion
- Although an empty class has no user-defined members, the compiler automatically generates 6 default member functions
- The size of an empty class is 1 byte, ensuring that different objects have different addresses
- Empty classes trigger empty base class optimization during inheritance, avoiding additional memory overhead
- You can explicitly control the generation of default member functions using
<span>= default</span>and<span>= delete</span>.