Considering this simple example:
class Base {
virtual void foo() {};
};
class Derived: public Base {
void foo() {};
};
Base *b = new Derived;
Derived *d = new Derived;
b->foo();
d->foo();
My question is: does a call to a member function declared as virtual in a base class (but not in the derived class) through a derived class pointer uses (and pay the price for) the vtable mechanism ? In the example b->foo()
uses the vtable mechanism to call the Derived::foo()
, but d->foo()
?
If yes, how circumvent this behavior: when using explicitly a Derived
pointer, I would like to directly call the Derived::foo()
method without paying the cost of the vtable, as if the base class does not exist?