2
동일한 이름이지만 다른 기본 클래스의 다른 서명을 가진 함수를 상속하는 경우 해당 함수를 호출하면 호출이 모호하다는 오류가 발생합니다. 단일 기본 클래스의 동일한 함수는 오류를 생성하지 않습니다. 왜 이런거야?다른 서명을 가진 메소드를 다중 상속하는 것이 모호한 이유는 무엇입니까?
첫번째 경우, 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)
번째 경우, 오류없이, 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;
}