C++ の仮想関数と仮想テーブルについて学習しています。しかし、なぜ動的バインディングが必要なのか理解できませんでした。コンパイラーには、関数呼び出しが派生関数用か基本関数用かを判断するためのすべての情報がありません。たとえば、次のようになります。
class Base1 {
public : virtual void foo()
{ cout << " Base foo \n"; }
};
class Base2 {
public : virtual void foo()
{ cout << " Base2 foo \n"; }
};
class derived : public base1, base 2 {
public : virtual void foo()
{ cout << " derived foo \n"; }
}
int main()
{
derived d;
Base2 *p = &d;
p->foo(); // why can't the compiler figure out that this
// is a function call to the derived function
// foo at compile time?
return 0;
}