Vitual functions are a key feature
of C++. Via dynamic binding, they provide a powerful mechanism to change
the semantics of a function at run time. To support the virtual function
mechanism, different schemas have been adopted. I shall discuss the method
used by Microsoft Visual C++ compiler (Model proposed by Martin O' Riordan).Whenever a class declares
a virtual function or is derived directly or indirectly from a class which
declares a virtual function, the complier adds an extra hidden member variable
which points to the virtual table. A virtual table is nothing but an array
of pointers to the virtual functions. The entries in the virtual table
are changed at run time to point to the correct function.Consider the following class
:

//Tweak
the virtual table pointer by changing the first 4 bytes (assuming a long
holds a pointer) memcpy(&DerivedObj,
&pVTable , sizeof(long));

//Call
the virtual function pBase->VirtFunc();

//Viola !! TweakFunc() is called return
0;}

A similar though slightly complex
technique is used for multiple inheritance and virtual inheritance. Might
as well write about them sometime. Please send me e-mail if you have any
question or comment about this article.