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-
virtualfunctions is decided at compile time. Hence, there is no good reason for an implementation to choose thevtableto dispatch a call to a non-virtualfunction. However, the standard doesn't prohibit an implementation from using avtableeven for non-virtualfunctions.@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