Empty Classes

You can declare empty classes, but objects of such types still have nonzero size.

Example

// empty_classes.cpp
// compile with: /EHsc
#include <iostream>

class NoMembers
{
};

using namespace std;
int main()
{
    NoMembers n;  // Object of type NoMembers.
    cout << "The size of an object of empty class is: "
         << sizeof n << endl;
}

Output

The size of an object of empty class is: 1

The memory allocated for such objects is of nonzero size; therefore, the objects have different addresses. Having different addresses makes it possible to compare pointers to objects for identity. Also, in arrays, each member array must have a distinct address.

Microsoft Specific

An empty base class typically contributes zero bytes to the size of a derived class.

END Microsoft Specific

See Also

Reference

Class-Type Objects