What are VTABLE and VPTR?

The vtable is a table of function pointers. It is maintained per class. vptr is a pointer to vtable. It is maintained per object (See this for an example). The compiler adds additional code at two places to maintain and use vtable and vptr. 

  • Code in every constructor. This code sets the vptr of the object being created. This code sets vptr to point to vtable of the class. 
  • Code with polymorphic function call (e.g. bp->show() in above code). Wherever a polymorphic call is made, the compiler inserts code to first look for vptr using a base class pointer or reference (In the above example, since the pointed or referred object is of the derived type, vptr of a derived class is accessed). Once vptr is fetched, vtable of derived class can be accessed. Using vtable, the address of the derived class function show() is accessed and called.

You may also like:  

We will soon be covering more C++.
 



Commonly Asked C++ Interview Questions | Set 1

Refer to the article C++ Interview Questions and Answers for the latest data.

Similar Reads

1. What are the differences between C and C++?

C++ is a kind of superset of C, most C programs except for a few exceptions (See this and this) work in C++ as well.  C is a procedural programming language, but C++ supports both procedural and Object Oriented programming.  Since C++ supports object-oriented programming, it supports features like function overloading, templates, inheritance, virtual functions, and friend functions. These features are absent in C.  C++ supports exception handling at a language level, in C exception handling, is done in the traditional if-else style.  C++ supports references, C doesn’t.  In C, scanf() and printf() are mainly used input/output. C++ mainly uses streams to perform input and output operations. cin is the standard input stream and cout is the standard output stream....

2. What are the differences between references and pointers?

Both references and pointers can be used to change the local variables of one function inside another function. Both of them can also be used to save copying of big objects when passed as arguments to functions or returned from functions, to get efficiency gain. Despite the above similarities, there are the following differences between references and pointers....

3. What are virtual functions – Write an example?

Virtual functions are used with inheritance, they are called according to the type of the object pointed or referred to, not according to the type of pointer or reference. In other words, virtual functions are resolved late, at runtime. The virtual keyword is used to make a function virtual....

4. What is this pointer?

...

5. Can we perform “delete this” operation?

The ‘this’ pointer is passed as a hidden argument to all nonstatic member function calls and is available as a local variable within the body of all nonstatic functions. ‘this’ pointer is a constant pointer that holds the memory address of the current object. ‘this’ pointer is not available in static member functions as static member functions can be called without any object (with class name)....

6. What are VTABLE and VPTR?

See https://www.geeksforgeeks.org/delete-this-in-c/...