How Virtual Table and _vptr works

Discussion in 'C++' started by Mridula, Mar 14, 2009.

  1. Mridula

    Mridula New Member

    Joined:
    Mar 5, 2008
    Messages:
    316
    Likes Received:
    19
    Trophy Points:
    0
    Occupation:
    S/w proffessional
    Location:
    Bangalore

    Introduction



    This article talks about how virtual table or vtable and _vptr works, with a pictorial representation.

    Background



    Virtual Table is a lookup table of function pointers used to dynamically bind the virtual functions to objects at runtime. It is not intended to be used directly by the program, and as such there is no standardized way to access it.

    Virtual Table:

    Every class that uses virtual functions (or is derived from a class that uses virtual functions) is given it's own virtual table as a secret data member.

    This table is set up by the compiler at compile time.

    A virtual table contains one entry as a function pointer for each virtual function that can be called by objects of the class.

    Virtual table stores NULL pointer to pure virtual functions in ABC.

    Virtual Table is created even for classes that have virtual base classes. In this case, the vtable has pointer to the shared instance of the base class along with the pointers to the classe's virtual functions if any.

    _vptr :

    This vtable pointer or _vptr, is a hidden pointer added by the Compiler to the base class. And this pointer is pointing to the virtual table of that particular class.

    This _vptr is inherited to all the derived classes.

    Each object of a class with virtual functions transparently stores this _vptr.

    Call to a virtual function by an object is resolved by following this hidden _vptr.

    Here is a simple example with a vtable representation.

    Here we have 3 classes Base, D1 and D2. Where D1 and D2 are derived from class Base.

    The Code



    Code:
    #include<iostream.h>
    
    class Base  
     {  
     public:  
        virtual void function1() {cout<<"Base :: function1()\n";};  
        virtual void function2() {cout<<"Base :: function2()\n";};  
        virtual ~Base(){};
    };  
       
    class D1: public Base  
    {  
    public:  
       ~D1(){};
       virtual void function1() { cout<<"D1 :: function1()\n";};
    };  
      
    class D2: public Base  
    {  
    public:  
       ~D2(){};
       virtual void function2() { cout<< "D2 :: function2\n";};  
    };  
    
    int main()
    {
      D1 *d = new D1;;
      Base *b = d; 
    
      b->function1();
      b->function2();
    
      delete (b);
      
      return (0);
    }
    
    output:
    D1 :: function1()
    Base :: function2()
    
    

    Here is a pictorial representation of Virtual Table and _vptr for the above code:


    [​IMG]

    Expalnation :

    Here in function main b pointer gets assigned to D1's _vptr and now starts pointing to D1's vtable. Then calling to a function1(), makes it's _vptr startightway calls D1's vtable function1() and so in turn calls D1's method i.e. function1() as D1 has it's own function1() defined it's class.

    Where as pointer b calling to a function2(), makes it's _vptr points to D1's vatble which in-turn pointing to Base class's vtable function2 () as shown in the diagram (as D1 class does not have it's own definition or function2()).

    So, now calling delete on pointer b follows the _vptr - which is pointing to D1's vtable calls it's own class's destructor i.e. D1 class's destructor and then calls the destrcutor of Base class - this as part of when dervied object gets deleted it turn deletes it's emebeded base object. Thats why we must always make Base class's destrcutor as virtual if it has any virtual functions in it.

    Bootom Line:
    Thats how the Run-time or Dynamic binding happens on calling virtual functions of different derived objects.
     
    Last edited by a moderator: Jan 21, 2017
  2. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    Nice article....
     
  3. imrantechi

    imrantechi New Member

    Joined:
    Feb 12, 2008
    Messages:
    116
    Likes Received:
    4
    Trophy Points:
    0
    Really good article...Please can you post one more article on how to VPTR, Vtable handles in case of Virtual base class.
     
    shabbir likes this.
  4. Mridula

    Mridula New Member

    Joined:
    Mar 5, 2008
    Messages:
    316
    Likes Received:
    19
    Trophy Points:
    0
    Occupation:
    S/w proffessional
    Location:
    Bangalore
    Thanks.
    Yes, I am planning to write an article on Vtable in case of Virtual Base class !!
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Article of the month competition nomination started here
     
  6. Mridula

    Mridula New Member

    Joined:
    Mar 5, 2008
    Messages:
    316
    Likes Received:
    19
    Trophy Points:
    0
    Occupation:
    S/w proffessional
    Location:
    Bangalore
    Hi Asadullah, Imrantechi,

    If you have liked the article please vote for me when the voting starts!!

    Many thanks
    Mridula.
     
  7. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    First start on the nominations
     
  8. Mridula

    Mridula New Member

    Joined:
    Mar 5, 2008
    Messages:
    316
    Likes Received:
    19
    Trophy Points:
    0
    Occupation:
    S/w proffessional
    Location:
    Bangalore
    Actually, I don't know the procedure for nominating the article etc.
    I would like to nominate all my articles of March.

    Do help me how to do it.

    Many thanks
    Mridula
     
  9. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Read there and just make a post stating you nominate the following.
     
  10. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  11. UIM star

    UIM star New Member

    Joined:
    Apr 16, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Still Learner
    Location:
    Dehradun
    good article complition
     
  12. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  13. somshekhar

    somshekhar New Member

    Joined:
    Aug 18, 2010
    Messages:
    4
    Likes Received:
    2
    Trophy Points:
    0
    Hi Mridula,
    Its a nice article but i want to add on to that ...

    When we create the object like

    Base* pBase = new Derived;
    Now as the pointer is of the base class but the object is created of the derived type then when we call a function

    pBase->Function1(); The first and the foremost thing happens is that first four bytes of object is extracted which stores the virtual table address and is set to the vptr and since the object is of the type derived it goes to the derived class v-table and calls the corrosponding function, and if a particular function is not overridden in the derived class then it keeps the reference from which class it has been derived..(most derived class not necessarily the base class, could be the class just above the derived class)....
     
    shabbir likes this.
  14. kgsfun

    kgsfun New Member

    Joined:
    Oct 16, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Excellent article, which makes me to understand about virtual functions and pointers very well. It was always a confusion, now I"m confident to answer the questions shooted by interviewers :)

    thanks a lot
     
  15. kelvincoper

    kelvincoper New Member

    Joined:
    Nov 24, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi Mridula,

    Very Nice article. I have a query here. If I write following code in main() function
    Code:
     
    D1 *d = new D1;
    d->function1();
    
    will it be a late binding call?

    Thanks,
     
  16. adityapandey1207

    adityapandey1207 New Member

    Joined:
    Dec 30, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Really nice article...Please can you post one more article on how to VPTR, Vtable handles in case of Virtual base class. and
    Plz can u explain what's normalized pointers and difference btwn near,far and huge pointers
     
  17. stamhaney

    stamhaney New Member

    Joined:
    Jan 14, 2013
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi Mridula,

    One question. In the pictorial representation of the vtable, you have shown that D1 and D2 have virtual destructors, whereas both D1 and D2 do not have virtual destructors., the desturctor of only base is virtual
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice