C++ How to multiple inherits from interfaces with different return types?
c++, multiple-inheritance, visual-studio-2012
Solution
It's not possible, but not because of the multiple inheritance. Ambiguity arises due to invalid overload of `foo` in class `C`. You can't have both `int foo()` and `void foo()` since return type is not part of function signature, thus the compiler won't be able to resolve the calls to `foo`. You can look at your interface as a union of both `A` and `B` classes, so logically the problem is already present before the actual inheritance. Since from compiler perspective `A` and `B` are 2 distinct and unrelated types, there is no problem while compiling them and the error is delayed until point of actual unification in class `C`.
See more here about function signatures and overloading: Is the return type part of the function signature?
Problem
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 ```