class classWithNoVirtualFunction
{
    public:
        int   a;
        void x () 
        {
            char c;
            c = 'a';
        }
};

class classWithOneVirtualFunction
{
public:
   int          a;
   virtual void x () {}
};

class classWithTwoVirtualFunctions
{
public:
   int          a;
   virtual void x () {}
   virtual void y () {}
};

int main() 
{
   cout << "\nclassWithNoVirtualFunction's size: "   << sizeof (classWithNoVirtualFunction);    
   cout << "\nclassWithOneVirtualFunction's size: "  << sizeof (classWithOneVirtualFunction);
   cout << "\nclassWithTwoVirtualFunctions's size: " << sizeof (classWithTwoVirtualFunctions);

   cout << "\nvoid*'s size : " << sizeof (void*);
} 

W.R.T the above mentioned code, Thinking in C++ states:

This situation needed a minumum of one data member. If there was no data people, the C++ compiler might have forced the objects to become a nonzero size because each object should have a definite address. Should you imagine indexing into a range of zero-sized objects, you’ll understand. A “dummy” member is placed into objects that will well be zero-sized.

My question: Now, I'm not in a position to understand what &lifier&lifier exactly how happens if there exists a class with zero people along with a virtual function. Please explain with a few programming good examples.

Each time a class consists of a minumum of one virtual function, the compiler must add RunTime Type Information to each one of the objects. The implementation will often give a single pointer to every object that describes a structure, based on the compiler and hidden from customers, having a pointer towards the type_info object and also the vtable employed for dynamic dispatch of functions.

Within the situation of the class without any non-static data people and a minimum of one virtual function, how big each object is how big the per-object RTTI information (one pointer), and since that's non-zero, the compiler won't add extra room. Exactly what the quote says is the fact that sizeof(T) != 0 for every types T, along with a type having a dynamic function trivially fits that requirement. Just with types that will have zero size, the compiler needs to create the item 1 char large.

The C++ standard doesn't define the entire layout of the class. This can be the compiler vendor. However the C++ standard does have guarantees. For instance, how big an entire object is definitely more than zero. The reason behind this rule is suggested at through the text you cited from Thinking in C++. Virtual functions of the simple class are usually implemented when it comes to one more hidden pointer member that identifies the object's dynamic type to ensure that you can invoke the right purpose of the object's dynamic type. Because this additional hidden member increases the size the course, the compiler does not have to put any padding inside to create the course possess a non-zero size.

However these are mainly implementation particulars you shouldn't be worried about an excessive amount of. As lengthy while you only depend around the guarantees the C++ standard makes, you ought to be fine.