Perhaps i have two interfaces with same function names and parameters, but with different return values:
struct A { virtual void foo() = 0; };
struct B { virtual int foo() = 0; };
How to define class C, that inherits this interfaces (if it’s of course possible)?
For example i write some pseudocode that doesn’t compiled:
// this code is fake, it doesn't compiled!!
struct C : A, B
{
// how to tell compiler what method using if referenced from C?
using void foo(); // incorrect in VS 2012
// and override A::foo() and B::foo()?
virtual void foo() { std::cout << "void C::foo();\n"; } // incorrect
virtual int foo() { std::cout << "int C::foo();\n"; return 0; } // incorrect
}
// ... for use in code
C c;
A &a = c;
B &b = c;
c.foo(); // call void C::foo() when reference from C instance
a.foo(); // call void C::foo() when reference from A instance
b.foo(); // call int C::foo() when reference from B instance
It’s not possible, but not because of the multiple inheritance. Ambiguity arises due to invalid overload of
fooin classC. You can’t have bothint foo()andvoid foo()since return type is not part of function signature, thus the compiler won’t be able to resolve the calls tofoo. You can look at your interface as a union of bothAandBclasses, so logically the problem is already present before the actual inheritance. Since from compiler perspectiveAandBare 2 distinct and unrelated types, there is no problem while compiling them and the error is delayed until point of actual unification in classC.See more here about function signatures and overloading: Is the return type part of the function signature?