2013-02-08 5 views
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; 
} 

답변

8

오버로딩은 동일한 범위에 정의 된 이름 사이에서 발생한다. 여러 개의 기본이 동일한 이름을 정의하면 정의가 다른 범위에 있으므로 과부하가 발생하지 않습니다. Derived에 선언문을 사용하여 두 개를 추가하면 두 이름을 Derived으로 가져올 수 있으며 과부하됩니다.