Overloading member function among multiple base classes

扶醉桌前 提交于 2019-12-10 18:02:07

问题


Basically I want to have multiple member functions with same name, but different signature, spread in multiple base classes.

Example:

#include <iostream>

struct A
{
    void print(int) { std::cout << "Got an int!" << std::endl; }
};

struct B
{
    void print(double) { std::cout << "Got a double!" << std::endl; }
};

struct C : A, B {};

int main()
{
    C c;
    c.print((int)0);

    return 0;
};

But I got this error on clang:

main.cpp:18:7: error: member 'print' found in multiple base classes of different types
    c.print((int)0);
      ^
main.cpp:5:10: note: member found by ambiguous name lookup
    void print(int) { std::cout << "Got an int!" << std::endl; }
         ^
main.cpp:10:10: note: member found by ambiguous name lookup
    void print(double) { std::cout << "Got a double!" << std::endl; }

Why is it ambiguous? Even with different number of arguments I get the same error.

Is there any workaround to get similar behavior?


回答1:


Use a using declaration in the derived class - it will fix your issues. It makes both overloads visible and viable to participate in the resolution.

struct C : A, B {
    using A::print;
    using B::print;
};

To answer why this is ambiguous: it is actually not about visibility, but about the inability to participate in the overload resolution, due to not being defined in the same scope. The using declaration pulls those methods in the C scope, so both of them become valid overloading resolution options.

Thanks to @Pete Becker for participating in this answer and pretty much creating this paragraph.



来源:https://stackoverflow.com/questions/51690394/overloading-member-function-among-multiple-base-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!