Virtual function and derived class
C++ compiler creates a lot of abstraction while constructing this vfptr, vtable and also while calling a virtual function of a class. To understand how this virtual function works, we have illustrated each section below with examples and with C language.
In the below example we have an example of a base class called CShape. This is an abstract base class. An abstract base class or ABC is a class which contains at least one pure virtual function.
Now we have derived two new shapes called CCircle and CRectangle from CShape.
Calling Virtual function using base class pointer
In object oriented environment we always use an abstract base class pointer to point the derived object and call base function and the corresponding derived class function get called. This is why we use virtual function, with base pointer we call the virtual function and the derived class function gets called.
The result is that, with base class pointer, without knowing the type of shapes, we called the Draw()[inside for loop] and with a magic it called the corresponding Draw() for each derived classes, like for Circle it called Draw() of CCircle and for rectangle it called Draw() of CRectangle, although for each time pointer is of CShape(base).
VPTR VTABLE Virtual function call - understanding using C
Now the point is, how it knows which function to call? Also how base pointer is related to the derived object's function address?
To elaborate this I want to use C language and some code using structure. Suppose we have a structure CShape. How I can relate the Draw() function to this structure?
The answer is simple, taking a function pointer inside the structure.
The problem with this approach is, each time I will add a new virtual function the size of the structure is going to increase by sizeof pointer. Like below if I add three virtual functions I am going to increase the size of structure by sizeof pointer x 3 thus each object of this type will consume this much of memory.
The solution is we can take one and only one pointer inside the structure to point to an array/table of function pointers. If we add more and more virtual functions, the size of the structure or object is not going to increase as it only holds the pointer to the table. But the size of the function pointer table will grow. This is acceptable as we increase the number of virtual function the table which records the location of the functions will surely going to increase. Now we have our picture ready as follows.
This vfptr is called pointer to vtable. This is the first hidden member of any C++ class which has one or more virtual function.
vfptr points to a list/table or structure of function pointers also called vtable. This list/table is a built-in feature of C++ compiler. It holds the address of virtual functions for a particular class. Each class has its own vtable and vtable assignment is done by compiler after constructor is called and vtable gets de-assigned before destructor is called.
Now we will have our CShape, CCircle, CRectangle C++ code rewritten in C. This code is very elaborate and there has been given all the steps which compiler does for object construction. This will give a clear understanding.
See here the call of the virtual function Draw(). Compiler calls the function pointer from the vtable through vfptr which is inside the object. Compiler calls a virtual function in three steps and they are -
- Get the vfptr,
- Goto the vtable and take the proper function pointer from the offset,
- Call the function using the pointer.
You may also like
Thanks for reading this answer. We hope you liked the content. These are some relevant contents people also visited. Hope you'll also wish to read these. Understand C++ vptr vtable virtual destructor Early binding Late binding virtual base class
About our authors: Team EQA
You have viewed 1 page out of 62. Your C++ learning is 0.00% complete. Login to check your learning progress.
Questions index C Questions C++ Questions Win32 MFC COM/DCOM DLL Questions
Compilers & Editors
Download Visual Studio Download XCode Download Visual Studio Code Android studio install sdk Eclipse installer Best C compilers IDEs
Development system setup
Windows media creation tool MSDN subscription Ubuntu virtualbox
New updated posts
Why learn C? Draw on printer Free alt softwares