why cant compile figure that this is a function call to derived function foo during compile runtime itself?
It can. And some compilers will convert that call to static binding.
And there are other scenarios when the compiler must use dynamic binding.
Which foo()
should get called in this function?
void function( Base* p )
{
p->foo();
}
It can't be determined*. Dynamic binding must be used.
*Edit: Based on the information I've given. :)