同じ名前で異なる基本クラスから異なるシグネチャを持つ関数を継承する場合、関数を呼び出そうとすると、呼び出しがあいまいであると主張するエラーが生成されます。単一の基本クラスの同じ関数はエラーを生成しません。どうしてこれなの?
最初のケース、http://ideone.com/calH4Q
#include <iostream>
using namespace std;
struct Base1
{
void Foo(double param)
{
cout << "Base1::Foo(double)" << endl;
}
};
struct Base2
{
void Foo(int param)
{
cout << "Base2::Foo(int)" << endl;
}
};
struct Derived : public Base1, public Base2
{
};
int main(int argc, char **argv)
{
Derived d;
d.Foo(1.2);
return 1;
}
prog.cpp: In function ‘int main(int, char**)’:
prog.cpp:27:7: error: request for member ‘Foo’ is ambiguous
prog.cpp:14:10: error: candidates are: void Base2::Foo(int)
prog.cpp:6:10: error: void Base1::Foo(double)
2番目のケース、エラーなし、http://ideone.com/mQ3J7A
#include <iostream>
using namespace std;
struct Base
{
void Foo(double param)
{
cout << "Base::Foo(double)" << endl;
}
void Foo(int param)
{
cout << "Base::Foo(int)" << endl;
}
};
struct Derived : public Base
{
};
int main(int argc, char **argv)
{
Derived d;
d.Foo(1.2);
return 1;
}