The sizeof() class with no virtual function inside it, is equal to the sum of all sizes of the member variable. For a class with one or more virtual functions the sizeof() class is the sizeof() class plus the sizeof(void *). This extra size is for the hidden member called vptr which is a pointer, points to vtable.

Example:

class C1
{
  short i;
  void funct(); 
};
class C2
{
  short i;
  virtual void funct();
};

C1: size of class C1 is 2 or sizeof(short).

C2: size of class C2 is sizeof(short) + sizeof(void *) that is 2 + 4 = 6 for 32bit compiler. Because this extra 4 byte is used as a hidden vptr member of the class.

About our authors: Team EQA

You have viewed 1 page out of 62. Your C++ learning is 0.00% complete. Login to check your learning progress.

#