top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What are VTABLE and VPTR Please explain in detail?

+5 votes
333 views
What are VTABLE and VPTR Please explain in detail?
posted Apr 13, 2016 by Mohammed Hussain

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+5 votes

To implement virtual functions, C++ uses a special form of late binding known as the virtual table which is kind of lookup table used to resolve function calls in case of late binding.

Every class that uses virtual functions or is derived from a class that uses virtual functions is given it’s own virtual table. This table is simply a static array that the compiler sets up at compile time. A virtual table contains one entry for each virtual function that can be called by objects of the class. Each entry in this table is simply a function pointer that points to the most-derived function accessible by that class.

Further, the compiler also adds a hidden pointer to the base class, which we will call VPTR which is set automatically when a class instance is created so that it points to the virtual table for that class. It also means that VPTR is inherited by derived classes.

Each object of a class with virtual functions, transparently stored in this VPTR.
Also, Call to a virtual function by an object is resolved by VPTR.

I hope it helps!!!

answer Apr 20, 2016 by Bhaskar Kalaria
...