Bloomberg LP Interview Question
Financial Software DevelopersI think the answer should be in terms of v-table and v-pointer.
A virtual-table is created for every class having one or more virtual functions. The
virtual table is usually implemented as an array of function pointers where each function pointer corresponds to the various implementations of the virtual function by the derived classes.
When an object of this class is created, it contains a virtual-pointer (v-pointer) which points to the start of v-table. Whenever a virtual function is invoked on this object, the runtime system resolves it by following the v-pointer to the v-table to invoke the appropriate function.
aka Dynamic binding.
- Anonymous July 27, 2009Typically, the pointer's type determines the function calls. However, virtual functions let the object (not pointer) determines function called.
The base type pointer, calls the method but the proper virtual function at runtime is called (dynamically).
Declare a function as virtual in base class, then override it in each derived class, just like redefining, new function must have same signature. If function declared virtual, can only be overridden. Once declared virtual, a function is virtual in all derived classes. It's a good practice to explicitly declare virtual though.
Dynamic binding chooses proper function to call of the virtual address table at runtime, but it only occurs off pointer handles. If function called from object, it uses that object’s definition.