Ambiguous when two superclasses have a member function with the same name, but different signatures

后端 未结 5 1993
野的像风
野的像风 2021-01-31 03:52
struct A {
    void f(int x) {}
};

struct B {
    template void f(T x) {}
};

struct C : public A, public B {};

struct D {
    void f(int x){}
    te         


        
5条回答
  •  既然无缘
    2021-01-31 04:30

    Consider this simpler example:

    struct A{
     void f(int x){}
    };
    
    struct B{
     void f(float t){}
    };
    
    
    struct C:public A,public B{
    };
    
    struct D{
     void f(float n){}
     void f(int n){}
    };
    
    
    int main(){
     C c;
     c.f(3);
    
     D d;
     d.f(3);
    }
    

    In this example, same as yours, D compiles but C does not.
    If a class is a derived one, member lookup mechanism behaves different. It checks each base class and merges them: In the case of C; Each base class matches the lookup ( A::f(int) and B::f(float) ). Upon merging them C decides they are ambiguous.

    For the case class D: int version is selected instead of float because parameter is an integer.

提交回复
热议问题