Search code examples
functioninheritancepolymorphismvirtualvtable

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!


Solution

  • 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