![]() |
Detail about How VPTR and Virtual table works
Originally written by Zeeshan Amjad
Assumption: 32-bit Machine. Here I am going to explain How Virtual table, Virtual pointer for Virtual functions are internally working. First we have understand memory layout. Example 1: How the class's memory layout Code: cpp
Sobj's Size = 8 obj 's Address = 0012FF7C Note: Any Plane member function does not take any memory. Example 2: Memory Layout of Derived class Code: cpp
obj1's Size = 8 obj1's Address = 0012FF78 obj2's Size = 12 obj2's Address = 0012FF6C Example 3: Memory layout If we have one virtual function. Code: cpp
obj's Size = 8 obj's Address = 0012FF7C Note: Adding one virtual function in a class takes 4 Byte extra. Example 4: More than one Virtual function Code: cpp
obj's Size = 8 obj's Address = 0012FF7C Note: Adding more virtual functions in a class, no extra size taking i.e. Only one machine size taking(i.e. 4 byte) Example 5: Code: cpp
a = 200 b = 10 If we Change the code as then Code: Cpp
a = 100 b = 200 Note: Who sits 1st place of Class : Answer is VPTR VPTR - 1st placed in class and rest sits after it. Example 6: Code: cpp
VPTR's Address 0012FF7C VPTR's Value 0046C060 NOTE: This VPTR's value is a address of Virtual table. Lets see in next Example. Example 7: Code: cpp
VPTR's Address 0012FF7C VIRTUAL TABLE 's Address 0046C0EC Value at first entry of VIRTUAL TABLE 0040100A Test: fun1 Example 8: Code: cpp
VPTR's Address 0012FF7C VIRTUAL TABLE 's Address 0046C0EC Value at first entry of VIRTUAL TABLE 0040100A Value at 2nd entry of VIRTUAL TABLE 004012 Example :9 Code: cpp
Test::fun1 Test::func1 Example 10: multiple Inheritance Code: cpp
Derive's Size = 12 Example 11: Calling Virtual Functions in case of Multiple Inheritance Code: cpp
Base1::fun Base1::func Base2::fun Base2::func Base3::fun Base3::func Drive::Fn Drive::Fnc |
Re: Detail about How VPTR and Virtual table works
|
Re: Detail about How VPTR and Virtual table works
I was really confused in virtual functions that how it's internall works. Now I got everything from your post. Thanks
|
Re: Detail about How VPTR and Virtual table works
Thanks Asadullah!!! very explanable and throughly about virtual pointer.
|
Re: Detail about How VPTR and Virtual table works
Can you take some more complex example of virtual functions like base class' virtual function has default arguments and derived class's virtual function has no default virtual function....
|
Re: Detail about How VPTR and Virtual table works
Example 10 is really excellent!!!
|
Re: Detail about How VPTR and Virtual table works
Good one!!!
|
Re: Detail about How VPTR and Virtual table works
I am new to C++. As I saw your post I fully understand the concept of VPTR, VTable etc.
******** No Issue at all. Thanks ********** |
Re: Detail about How VPTR and Virtual table works
Really nice info of Virtual function. I was really confused with virtual function having multiple inheritance
|
Re: Detail about How VPTR and Virtual table works
really this is an excellent article ,there was a lot of confusion in vptr
this article has really helped me understand the concept better |
| All times are GMT +5.5. The time now is 13:05. |