devxlogo

When does a virtual member function imposes runtime overhead?

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
See also  Why ChatGPT Is So Important Today
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist