Bloomberg LP Interview Question
Software Engineer / Developers"If not found lookup is performed in the vtable of the immediate base class and so on"
That cant happen, the virtual members of the base if not overridden in the derived class, the address of the base member function is stored in the vtable of the derived class.
Eg:
if the base class virt2 function is not overridden then the derived class vtable will have
&Der::virt1, &Base::virt2
For each class which has at least 1 virtual table, the compiler build a virtual table with the addresses of those virtual functions.
In the constructor, each object gets a VPTR, which is a pointer to the VTable.
In inheritance, the base class's consrtuctor is called first. It creates the VPTR and initializes it to the Base VTABLE.
The derived class consrtuctor is called then, and updates the VPTR to point the Derived VTABLE.
The final state of the VPTR is determined by the consrtuctor who called last.
When a Base pointer points to a derived object and calls a virtual method, the compiler translate this call to something like:
pBase -> VPTR -> right place in the VTABLE -> right method.
There is a vtable for each class that has atleast one virtual function. The first member of each object of a class is the pointer to the v-table (This member is not visible to the programmer but is an internal representation). So when the base class pointer points to the derived class, it has the pointer to the v-table that has an entry for the method to call (which is either the method in the base class if the derived class does not implement it or the method in the derived class itself).
For this question, I think they are asking for the Virtual Table Implementation. I have surfed the web but couldn't find much info on this.
Here is an example of what they were asking for.
class A {
virtual void foo() { }
void bar() {}
}
class B {
foo() {}
bar() {}
}
int main(void) {
A *aptr = new B();
aptr->foo(); //This would call B's foo()
aptr->bar(); //This would call A's bar()
return 1;
}
The question comes down to, how does the compiler model the Virtual Table such that it knows to call B's foo() and A's bar(). I have read quite a few C++ books and none really go into depth about this. Even if they did, I wouldn't remember but I know that "The Design and Evolution of C++" book written by Bjarne Stroustrup does mention this. It has some complicated rules dealing with Virtual Table Implementation. I need to go back and read this book.
Because when you say aptr->foo, the compiler translates it into
foo(aptr)
Another example
class X {}
int main()
{
X xx;
xx.foo(); //this is essesially foo(&xx);
return 0;
}
remember that every function gets passsed a "this" pointer, which is the address of the calling object. This is why we can't have static virtual functions, because they have no "this" pointer passed to them.
- A vtable is created for each class.
- Samba December 21, 2005- That means if B derives from A, there will be two vtables
- the vtable is populated during compilation
- All objects of B have a pointer (vptr) to the vtable of B
- All objects of A have a pointer to the vtable of A
- all functions marked "virtual" are looked in the vtable of the actual object created, if found they are invoked
- If not found lookup is performed in the vtable of the immediate base class and so on