Click here to Skip to main content
15,886,071 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am trying to understand the internals of vtable & vptr implementation and I have some question. If anybody can explore in details about all these.

1: When does vtable and vptr getting created? Is it compile time or at Run time?
2: how can we print the address of vptr and containt of vptr, i.e. the address of vtable? Is it possible to print these addresses. if no then why?

Waiting for quick reply .............
Posted

1) By the very first principles, virtual table is always created during compile-time. There is nothing dynamic in it. It's function call dispatching is dynamic, happens during run-time — thanks to virtual table.

2) Accessing the virtual table pointer is not a trivial problem. First thing to understand is: in all OOP languages, there can be several different pointers pointing to virtual table, depending on compile type used for access to the object. Usually this is the same pointer: derived classes only adds up to the virtual table of the base class. You always get the same pointer as a result of dynamic cast. This is not exactly so in C++; this situation if highly complicated by multiple inheritance. Apparently different base classes needs different pointers to the virtual table. In a typical implementation, it is still the same virtual table, only accessed "from different points of view". I suspect the physical layout are very similar in different implementation, but they don't have to be the same. Strictly speaking, the layout is implementation-dependent, as Kurt pointer our correctly.

—SA
 
Share this answer
 
v3
Comments
CPallini 16-May-11 4:22am    
My 5.
Sergey Alexandrovich Kryukov 16-May-11 4:31am    
Thank you, nice to get a good vote from a real C++ person.
--SA
Niklas L 16-May-11 6:46am    
Fived. Again.
Sergey Alexandrovich Kryukov 16-May-11 6:50am    
Thank you, Niklas.
--SA
Espen Harlinn 16-May-11 16:00pm    
Nice reply, my 5
DISCLAIMER: this topic is very compiler specific


the vtable is a static member as such it created at compile time(1 per class) this logic applies to the vptrs as well

accessing the vtable is a bit tricky (and very compiler dependent) but it can be done:

Take a look at this code (for VC++ only)

aobj *temp = new aobj(); //NOTE MUST CONTAIN NON PURE VIRTUAL FUNCTION

int* vptr =  *(int**)temp; //get the  vptr

//assmebly code to make "temp" as the "this" ptr
__asm
{
  mov ecx, temp
}

 ((void (*)()) vptr[0] )(); //this will call the first function in vtable
 
Share this answer
 
v2
Comments
Gerben Jongerius 13-May-11 8:50am    
You might also be interested in reading virtual tables on how virtual tables work.
Sergey Alexandrovich Kryukov 13-May-11 13:28pm    
This is correct answer, my 5.

However, it needs some clarification. The answer to first question is very certain, but second one -- much less trivial due to C++ ***multiple inhheritance***.

Please see my answer.
--SA
CPallini 16-May-11 4:22am    
My 5.
Niklas L 16-May-11 6:47am    
Fived!
Kurt Degiorgio 16-May-11 8:36am    
Thanks!! I agree with you SA multiple inheritance will introduce further complications.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900