alk3ovation
alk3ovation

Reputation: 1232

Non virtual functions in a class with virtual functions

Quick question: Do non virtual functions incur the cost of a vtbl lookup in classes with other virtual functions? For example:

Class A
{ 
  virtual void init();
  void update();
};

Class B : public A
{
  void init();
}

A* = new B();
A->init();

while(true)
{
  A->update();
}

Will the call to update incur the cost of a vtbl lookup? This code is very performance sensitive so I need to avoid virtual function calls. Thanks!

Upvotes: 3

Views: 127

Answers (2)

Bernard Chen
Bernard Chen

Reputation: 6567

No. update() will not be in the vtable. Wikipedia had this to say: "Note that those functions not carrying the keyword virtual in their declaration ... do not generally appear in the vtable. There are exceptions for special cases as posed by the default constructor."

http://en.wikipedia.org/wiki/Virtual_method_table

Upvotes: 3

a1ex07
a1ex07

Reputation: 37364

If you don't call virtual functions inside your non-virtual functions, no vtbl lookup involved.

Upvotes: 1

Related Questions