In this example below, Type has a virtual method so it has a vtable. However, Type::Bar() is not virtual. When calling Bar(), does the call also go through the vtable mechanism, or will it only apply to Foo()?
struct Base {
virtual void Foo() {}
}
struct Type : Base {
void Foo() override {}
void Bar() {}
}
Base* b = new Type();
Type* t = static_cast<Type*>(b);
t->Bar(); // Does this use a vtable?
The function to call for non-
virtual
functions is decided at compile time. Hence, there is no good reason for an implementation to choose thevtable
to dispatch a call to a non-virtual
function. However, the standard doesn't prohibit an implementation from using avtable
even for non-virtual
functions.@EJP said it better:
The standard doesn't require an implementation to use vtable for virtual functions. It's an implementation detail. No sane implementation using vtables would waste space in them by including non-virtual functions