Not at all. Consider this example:
struct A {
virtual void f() = 0;
};
struct B : A {
virtual void f() { std::cerr << "In B::f()\n"; }
};
struct C : A {
virtual void f() { std::cerr << "In C::f()\n"; }
};
static void f(A &a)
{
a.f(); // How do we know which function to call at compile time?
}
int main(int,char**)
{
B b;
C c;
f(b);
f(c);
}
When the global function f
is compiled, there is no way to know which function it should call. In fact, it will need to call different functions each time. The first time it is called with f(b)
, it will need to call B::f()
, and the second time it is called with f(c)
it will need to call C::f()
.