Click here to Skip to main content
15,881,881 members
Please Sign up or sign in to vote.
3.32/5 (4 votes)
See more:
class empty
{
};
int main()
{
empty obj;
cout<<size(obj);
return 0;
}
Posted
Comments
Sergey Alexandrovich Kryukov 2-Oct-14 1:15am    
What is that, "size of a class"? You are showing a size of some object. :-)
What is the size you found, 1 byte?
Yes, you can create objects of an empty class. Perhaps, just because of its emptiness, some placeholder byte is needed.

In fact, this is a pretty interesting question; I voted my 5.

—SA
[no name] 2-Oct-14 2:51am    
Sergey - the question is answered by the code given.

Shouldn't that be sizeof, not size?
Richard MacCutchan 2-Oct-14 4:03am    
A class will always have some basic control data so its size will never be zero. And, yes, you can create objects of an empty class, but they will not be much use.
jsolutions_uk 2-Oct-14 4:07am    
There is a good explanation
here.

As I read it ... An "empty" object has to have some size as if it was 0 size, one would not be able to use pointer arithmetic on arrays of an empty object .... or something along those lines :)

1 solution

Bjarne Stroustrup, the creator of the C++ language, answers the question here: Why is the size of an empty class not zero?[^]
 
Share this answer
 
v2
Comments
nv3 2-Oct-14 9:17am    
Good link, thanks!
CPallini 2-Oct-14 9:51am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900