#Previous question #Index of C++ Questions #Next question

vfptr, vftable, virtual functions

C++ compiler creates a hidden class member called virtual-pointer or in short vfptr when there are one or more virtual functions. This vfptr is a pointer that points to a table of function pointers. This table is also created by compiler and called virtual function table or vftable. Each row of the vftable is a function pointer pointing to a corresponding virtual function.

b.vfptr ->Vftable[0] ->base::funct1()
Vftable[1] ->base::funct2()

To accomplish late binding, the compiler creates this vftable table for each class that contains virtual functions and for the class derived from it. The compiler places the addresses of the virtual functions for that particular class in ‘vftable’.

When virtual function call is made through a base-class pointer, the compiler quietly inserts code to fetch the VFPTR and look up the function address in the VFTABLE, thus calling the right function and causing late/dynamic binding to take place.

Source Code

class base 
  virtual void funct1(void);
  virtual void funct2(void);
base b;
/* C++ Compiler internally Does this*/
b.vptr = address of b.vtable;
10  b.vtable[0]= &base::funct1;
11  b.vtable[1]= &base::funct2;

Pictorial View

vptr, vtable, virtual functions

You have viewed 1 page out of 62. Your C++ learning is 0.00% complete. Login to check your learning progress.

 Vote 1

Similar topics related to this section

object-oriented, C vs C++, C++ vs Java, encapsulation, constructor, overloaded constructor, destructor, destructor overloaded, copy constructor, copy constructor, deep copy, deep copy vs shallow copy, singleton, default access modifier, default access modifier, default access modifier, abstraction, THIS pointer, static function, THIS pointer, a static function, scope resolution operator, inline function, private vs protected, const function, polymorphism, operator overloading, function overloading, overriding, prefix vs postfix, friend class, friend function, sizeof class with virtual, vfptr and vftable, vfptr and vftable using C, early binding and late binding, inheritance, virtual function call from a constructor/destructor, virtual destructor, virtual base class, virtual base class, exception, stack unwinding, exception handling, try-catch block, namespace, mutable variable and const function, mutable keyword, explicit, access a member function, object slicing, ctor sequence of constructor, dtor sequence of destructor, virtual destructor, print the type, dynamic casting, static casting, dynamic and static casting, const casting, reinterpret casting, static and reinterpret casting, template class, template vs macro,

#Object-Oriented Programming in C++ (4th Edition)
#Programming: Principles and Practice Using C++ (2nd Edition)
#The C++ Programming Language, 4th Edition
#Professional C++ Paperback by Nicholas A.Solter, Scott J.Kleper
#Effective Modern C++: 42 Specific Ways to Improve Your Use of C++11 and C++14