Tuesday, April 3, 2012

what is virtual table or Vtable in C++?

In C++ for implementing virtual functions, compiler uses Virtual table or simply Vtable. This is also called virtual function table or virtual method table.  Lets see what is it and how it works.

As you know virtual functions are used for run time binding or function overriding. If a class contains a virtual function, you will get one virtual table for that class. This applies for derived classes also. So for each class which contains atleast one virtual function, we will be having corresponding Vtable. Compiler create this Vtable at the time of compilation. Vtable is a static array which contians one entry for each virtual fucntion that can be called by object. Each entry in the Vtable is a function pointer to the "most derived" function.

The compiler will also add hidden pointer called *_vptr to the Vtable for the base class. *_vptr will be inherited to the derived class Vtable also. *_vptr pointer adds to the Vtable automatically when instance is creatd. See the below sample code snippet and  image for more clarity.

class vtBase
{
    public:
        virtual void display()
        {
            cout<<"this is dispaly function"<<endl;
        }
        virtual void displayMore()
        {
            cout<<"this is dispalyMore function"<<endl;
        }

};

class derivedD:public vtBase
{
    public:
        void display() // display function overriding
        {
            cout<<"this is derived display function"<<endl;
        }
};
class derivedDD:public vtBase
{
    public:
        void displayMore() //displayMore function overriding
        {
            cout<<"this is derived displayMore function"<<endl;
        }
};


In the above code there is base claass vtBase with two virtual functions display() and displayMore(). There are two derived classes in which derivedD is override the function display() with new definition and derivedDD is overrdes the definition for displayMore().  The corresponding Vtable is given below.

Virtual Table
In the above Vtable, base class *_vptr is pointing to the base class vtBase table and its member functions are function pointers to the base class.  And derived class *_vptr are pointing to the derived calls Vtable pointer. And in derived class derivedD , you will have two member functions derived from base class, in that one member function display() is overrides with new definition. So its "pointer pointing to the derived member" function and displayMore function "pointer points to the base class" pointer. Similarly for the derived classe derivedDD, but here displayMore()  is overrides with new definition. So this function pointer points to derived class and display()  function pointer points to the base class.

what happens when object creation:

main()
{
    derivedD D; // creating derived object
    vtBase *bPtr=&D; // assining to base pointer
    bPtr->display();
}

Output:
this is derived display function

In the above main fucntion,  in line no. 5,  by seeing this, compiler finds display()  is virtual function, and then it gets vPtr->display() value as derivedD virtual table from there it looks for the display() function pointer. all this happens in three steps
  • finding function as virtual
  • calling _vptr for getting Vtable address
  • finding the pointer for the function

 Using this Vtable process the compiler resolves to the proper virtual function even if you use pointers and references. You can also access the Vtable pointer by using _vptr. see the sample code snippet below.
main()
{
    derivedD D; // creating derived object
    vtBase b; // creating base object
    vtBase *bPtr=&D; // assining to base pointer
    bPtr->display();
    cout<<"_vptr is "<<bPtr->_vptr<<endl; //accessing the _vptr
    bPtr=&b; // assining to base pointer
    bPtr->display();
    cout<<"_vptr is "<<bPtr->_vptr<<endl;//accessing the _vptr
}

Output:
this is derived display function
_vptr is 0x400cb0
this is dispaly function
_vptr is 0x400d30


2 comments:

Anonymous said...

Very clear and detailed explanation. thanks A lot...

Sex Places in Montreal said...

Thanks for postingg this

Popular Posts