Q:

Why is the size of an empty class not zero in C++?

0

Why is the size of an empty class not zero in C++?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

The standard does not allow objects of size 0 since that would make it possible for two distinct objects to have the same memory address. That’s why even empty classes must have a size of (at least) 1 byte.

Example,

#include<iostream>
using namespace std;
class Test
{
 //empty class
};
int main()
{
    cout << sizeof(Test);
    return 0;
}

Output: 1

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Interview Questions and Answers(2022)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
What is a constructor in c++?... >>
<< What are the differences between a class and a str...