 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the size of an object of an empty class in C++?
The size of an object of an empty class in C++ is 1 byte as it allocates one unique address to the object in the memory. The size can not be 0, as the two objects can not have same memory allocation.
In this article, we will see an example of checking the size of an object of an empty class in C++.
Demonstrating Size of an Empty Class
In this example, we have two C++ classes. One class is an empty class while other is not an empty class. We have printed the size of objects of both the classes as output.
#include <iostream> using namespace std; class Empty {}; class NotEmpty { public: int a = 4; }; int main() { cout << "Size of Empty class: " << sizeof(Empty) << endl; Empty obj; cout << "Size of Empty object: " << sizeof(obj) << endl; cout << "Size of NotEmpty class: " << sizeof(NotEmpty) << endl; NotEmpty obj2; cout << "Size of NotEmpty object: " << sizeof(obj2) << endl; return 0; }  Output
The output of the above code is given below:
Size of Empty class: 1 Size of Empty object: 1 Size of NotEmpty class: 4 Size of NotEmpty object: 4
Advertisements
 