C++ 如何从具有不同返回类型的接口进行多个继承

C++ How to multiple inherits from interfaces with different return types?

本文关键字:接口 继承 返回类型 C++      更新时间:2023-10-16

也许我有两个接口具有相同的函数名称和参数,但具有不同的返回值:

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类的联合,因此从逻辑上讲,问题在实际继承之前就已经存在。由于从编译器的角度来看,AB 是 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建议的概括。