Size of empty class is always 1 byte.
Reason is, in order to differentiate one object to another object, the size of empty class is always 1 byte.
See the below c++ code snippet. Here there are three object created, each object size is 1. Their address is +1 byte for each object.
#include
using namespace std;
// Empty class declaration
class Student
{
};
int main() {
// Object creation
Student s1,s2,s3;
// Getting sizeof the object
cout << "Size of empty class Student for object s1 " << sizeof(s1) << endl;
cout << "Size of empty class Student for object s2 " << sizeof(s2) << endl;
cout << "Size of empty class Student for object s3 " << sizeof(s3) << endl;
// Getting address of the object
cout << "Address of object s1 " << &s1 << endl;
cout << "Address of object s2 " << &s2 << endl;
cout << "Address of object s3 " << &s3 << endl;
return 0;
}
output
Size of empty class Student for object s1 1
Size of empty class Student for object s2 1
Size of empty class Student for object s3 1
Address of object s1 0x7ffc4fb8572d
Address of object s2 0x7ffc4fb8572e
Address of object s3 0x7ffc4fb8572f