Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are C++ classes allowed to have zero data members?

Tags:

c++

question about c++ why minimal number of data members in class definition is zero

i think it should be one , i.e pointer to virtual table defined by compiler

thanks a lot

like image 221
user408015 Avatar asked Aug 01 '10 17:08

user408015


2 Answers

It is often useful to have a class with no data members for use in inheritance hierarchies.

A base class may only have several typedefs that are used in multiple classes. For example, the std::iterator class template just has the standard types defined so that you don't need to define them in each iterator class.

An interface class typically has no data members, only virtual member functions.

A virtual table has nothing to do with the data members of a class.

like image 69
James McNellis Avatar answered Sep 17 '22 17:09

James McNellis


Well, actually C++ mandates that all classes must occupy some space (You need to be able to generate a pointer to that class). They only need a pointer to a vtable though, if the class is polymorphic. There's no reason for a vtable at all in a monomorphic class.

like image 44
Billy ONeal Avatar answered Sep 16 '22 17:09

Billy ONeal