Vtable and Vptr !!!

Vtable or virtual table is similar to static array, To implement the virtual function C++ uses a special form of late binding as the virtual table. Virtual table is a look up table which is used to resolve the virtual function be late binding manner.

A virtual table created by the compiler for the class which is having virtual function or class which is derived from the class which used the  virtual function in both of the cases a separate vtable created for respective class. A vtable contains the entry of each virtual function in that class for which vtable is created and these virtual function can be called by the object of the respective class.

Each entry in the virtual table is a function pointer(having the starting address of the function from which function get called) that points to the most derived function accessible by that class.

Compiler also add a hidden pointer to the base class it is called vptr. It is created when an instance is created of that class it points to the vtable of that class, Vptr is inherited by the derived class.



  1. When an instance of type Base is created a vptr is set to points to the virtual table of Base(vtable Base).
  2. When an object of type D1 and D2 is created vptr is set to points to vtable D1 and D2 respectively

In the above scenario each vtable will have two entires:

One for virtual function1() and another for virtual function2().

See the diagram:



528 total views, 2 views today

Posted in

Leave a Reply