Bloomberg LP Interview Question
Financial Software Developershttp://www.go4expert.com/forums/showthread.php?t=8403
This would help us understand 4 byte vptr in a concise manner.
The actual location of the vptr pointer is not guaranteed since it's not a part of the C++ standard. It's up to C++ compiler how to implement polymorphic behavior. Virtual table is a common way of doing so.
When a virtual function is invoked the compiler replaces it with code like this:
(*pObj->vptr[idx])(arg);
where:
pObj - pointer the object
idx - the virtual function's unique id
arg - argument passed to the function
If the function is called through the object's name (not a pointer or reference) this call can be easily optimized so that vptr will not be used during run-time.
A good reference is Meyers 'More Effective C++ (35 New Ways...)', Item 24: Understand the costs of virtual functions
Virtual functions are used to extend the polymorphic behavior. You can use them when you want to call the implementation of the derived class member function using a base class pointer.
- Ashish October 17, 2009VPT:- every class has its own virtual function pointer table. It consist of its virtual functions in an organized way which helps compiler to determine which function to call at run time.
e.g
class A{
public:
virtual void print(){cout<<"A";}
};
class B:public A{
public:
void print(){cout<<"B";}
};
void main()
{
A* a;
B b;
a=&b;
a->print();
}