Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested empty class

Tags:

c++

I have the following code

class nest_empty
{
   class empty{};
};

Will the size of nest_empty be 1 (on my implementation sizof an empty class is 1)? If yes why? Can nest_empty be considered as an empty class?

EDIT:

class nest_empty
{
   class empty{};
   empty d;
};

Will the size of nest_empty still be 1? If yes why?

like image 865
Prasoon Saurav Avatar asked Nov 23 '10 13:11

Prasoon Saurav


People also ask

Can we have empty class in C++?

In C++, the Size of an empty structure/class is one byte as to call a function at least empty structure/class should have some size (minimum 1 byte is required ) i.e. one byte to make them distinguishable.

What is an empty class?

Empty class means, a class that does not contain any data members e.g. int a , float b and string etc. However, a class can contain member functions. In below example, both classes will be called as an empty class. If we calculate the size of both classes, output will be 1 byte for both.

Can a class be empty in C#?

Yes, a class which extends another class can be empty. You're code compiles and runs, and therefore is allowed by the c# compiler.

What are nested classes in C++?

Nested Classes in C++ A nested class is a class which is declared in another enclosing class. A nested class is a member and as such has the same access rights as any other member. The members of an enclosing class have no special access to members of a nested class; the usual access rules shall be obeyed.


3 Answers

Your first version of nest_empty is an empty class (no non-static data members, and no non-empty bases), so if they have size 1 in your implementation, it has size 1.

"Why" is because empty classes have size 1 on your implementation, which in turn is because they can't have size 0 (the standard forbids it), and your implementer has chosen 1.

Your second nest_empty is not an empty class (it has a non-static data member). It could legally have size 1, since its only non-static data member, d, is of type empty, which is an empty class and hence presumably of size 1.

I can't tell you whether it actually will have size 1 on your implementation, though. Ask your compiler.

like image 56
Steve Jessop Avatar answered Sep 21 '22 19:09

Steve Jessop


Yes. empty is just in the namespace of nest_empty.

To be clearer, the line class nest_empty{}; simply defines nest_empty. It does not declare any member in empty.

like image 26
lijie Avatar answered Sep 20 '22 19:09

lijie


It's not mandatory for sizeof(nest_empty) to be 1, but it won't be zero.

$9.3 says: Complete objects and member subobjects of class type shall have nonzero size.

This is needed because if you create an array of nest_empty, each one has to have a different address from the other.

EDIT

Most probably, sizeof(nest_empty) will yield the same result in both version, but that it's not mandated. The only thing the standard says is that empty class will have nonzero size.

like image 27
Simone Avatar answered Sep 23 '22 19:09

Simone