Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/articles/cpp-tutorials/)
-   -   How Virtual Table and _vptr works (http://www.go4expert.com/articles/virtual-table-vptr-t16544/)

Mridula 14Mar2009 17:56

How Virtual Table and _vptr works


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


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: Cpp


class Base 
    virtual void function1() {cout<<"Base :: function1()\n";}
    virtual void function2() {cout<<"Base :: function2()\n";}
    virtual ~Base(){};
class D1: public Base 
   virtual void function1() { cout<<"D1 :: function1()\n";};
class D2: public Base 
   virtual void function2() { cout<< "D2 :: function2\n";}

int main()
  D1 *d = new D1;;
  Base *b = d;


  delete (b);
  return (0);

D1 :: function1()
Base :: function2()

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

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.

asadullah.ansari 17Mar2009 09:50

Re: How Virtual Table and _vptr works
Nice article....

imrantechi 17Mar2009 10:13

Re: How Virtual Table and _vptr works
Really good article...Please can you post one more article on how to VPTR, Vtable handles in case of Virtual base class.

Mridula 17Mar2009 11:14

Re: How Virtual Table and _vptr works
Yes, I am planning to write an article on Vtable in case of Virtual Base class !!

shabbir 6Apr2009 15:02

Re: How Virtual Table and _vptr works
Article of the month competition nomination started here

Mridula 6Apr2009 18:56

Re: How Virtual Table and _vptr works
Hi Asadullah, Imrantechi,

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

Many thanks

shabbir 6Apr2009 19:00

Re: How Virtual Table and _vptr works
First start on the nominations

Mridula 6Apr2009 19:19

Re: How Virtual Table and _vptr works
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

shabbir 6Apr2009 19:59

Re: How Virtual Table and _vptr works
Read there and just make a post stating you nominate the following.

shabbir 17Apr2009 14:15

Re: How Virtual Table and _vptr works
Vote for this article as Article of the month

UIM star 25Apr2009 00:35

Re: How Virtual Table and _vptr works
good article complition

shabbir 29Apr2009 17:54

Re: How Virtual Table and _vptr works
Undoubtedly Winner of Article of the month for March 2009

somshekhar 18Aug2010 10:46

Re: How Virtual Table and _vptr works
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)....

kgsfun 17Oct2010 03:40

Re: How Virtual Table and _vptr works

Originally Posted by asadullah.ansari (Post 44443)
Nice article....

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

kelvincoper 24Nov2010 12:58

Re: How Virtual Table and _vptr works
Hi Mridula,

Very Nice article. I have a query here. If I write following code in main() function

D1 *d = new D1;

will it be a late binding call?


adityapandey1207 15Jan2011 22:52

Re: How Virtual Table and _vptr works
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

stamhaney 14Jan2013 22:48

Re: How Virtual Table and _vptr works
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

All times are GMT +5.5. The time now is 04:03.