C++ 如何从具有不同返回类型的接口进行多个继承
C++ How to multiple inherits from interfaces with different return types?
也许我有两个接口具有相同的函数名称和参数,但具有不同的返回值:
struct A { virtual void foo() = 0; };
struct B { virtual int foo() = 0; };
如何定义继承此接口的类 C(如果当然可能的话)?例如,我编写了一些未编译的伪代码:
// 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
这是不可能的,但不是因为多重继承。歧义是由于类 C
中无效的foo
重载而产生的。不能同时具有int foo()
和void foo()
,因为返回类型不是函数签名的一部分,因此编译器将无法解析对foo
的调用。您可以将接口视为 A
类和B
类的联合,因此从逻辑上讲,问题在实际继承之前就已经存在。由于从编译器的角度来看,A
和 B
是 2 种不同且不相关的类型,因此在编译它们时没有问题,并且错误会延迟到类 C
中的实际统一点。
在此处查看有关函数签名和重载的更多信息:返回类型是函数签名的一部分吗?
您可以明确限定。
class C : A, B {
public:
void A::foo() { ... }
int B::foo() { ... }
};
编辑:
这似乎是 MSVC 扩展,而不是标准扩展。
你可以
做的是将所有共享的东西放到一个virtual
基中,比如说C_base
,并覆盖辅助类中相互冲突的virtual
函数。由于C
的共享内容在C_base
中很容易获得,因此本质上就像在最派生的C
中一样。最后,你C
类只是因为类把事情放在一起:
struct A { virtual void foo() = 0; };
struct B { virtual int foo() = 0; };
struct C_base { int all_data; };
struct A_aux: virtual C_base, A {
void foo() { std::cout << "A_aux::foo()n"; }
};
struct B_aux: virtual C_base, B {
int foo() { std::cout << "B_aux::foo()n"; return 0; }
};
struct C : A_aux, B_aux
{
using A_aux::foo;
};
使用相同的签名不可能覆盖虚函数的返回类型(正如@icepack的答案所解释的那样)。
另一种方法可能是让模板基类声明foo()
,并将返回类型指定为模板参数,并为具体的返回参数类型提供专用化。
这将是@Dietmar Kühl建议的概括。
相关文章:
- Qt Quick - 如何在 qml 接口中使用继承自 QQuickPaintedItem 的 c++ 类?
- C++接口继承不同的参数方法
- c++ com 如何继承 IUIAutomationPropertyChangedEventHandler 接口
- C++ 使用接口和默认实现继承
- C++ 通过接口使用从其他几个类继承的类
- 如何在C++中访问 DirectWrite 继承的接口?
- 接口的钻石继承(C++)
- 将实现从继承的 CRTP 注入到继承的接口类
- C++:继承自基类和具有公共基接口的多个接口
- 接口实现和私有继承之间的交互
- 替代与djinni的接口继承
- C++ 接口类继承自具体类是否合理
- C++接口、继承、多态性
- C++与工厂的接口继承
- C 钻石接口继承
- C 接口继承
- 企业架构师 - 从类图中的接口继承方法
- 虚拟函数未从接口继承
- 抽象接口继承
- 嵌套接口继承实现