When does a virtual member function imposes runtime overhead?

Dynamic binding, for example, having a virtual member(s), is one of the basic principles of OO programming. If the compiler can resolve a call to a virtual member function statically, no extra overhead is incurred and it can even be inlined. In other words, it is as efficient as any ordinary function call, provided that the compiler can decide at compile time which object’s virtual function is the one to be invoked. However, in certain circumstances it can’t be done, and as a result, a virtual call imposes a slight run-time overhead, for example, when a virtual is called from a pointer or a reference to an object:

 class V { //a polymorphic class	virtual void show() const { cout<<"I'm V<v.show(); 	//v is a reference, can be bound to a V or object derived
//from V; call resolution is postponed to run-time.pV->show(); //pV is a pointer to a base class, can point to a derived
//object as well, so call resolution is postponed to run-
//time.
}//end f
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: