Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: C++
Expertise: All
Apr 17, 1998



Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js

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<<endl; } 

class W : public V {
	void show() const { cout<<"I'm W<<endl; }

void f(V & v, V *pV) {

W w;	//local object 
w.show(); 	//call resolved statically to W::show(); no overhead.
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-
}//end f
Danny Kalev
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date