实现两个函数具有相同的名称,但不同的非协变返回类型由于多个抽象基类 [英] Implement two functions with the same name but different, non-covariant return types due to multiple abstract base classes
问题描述
如果我有两个抽象类定义一个纯虚函数具有相同的名称但不同的非协变返回类型,我怎么从这些派生和定义一个实现为他们的功能?
If I have two abstract classes defining a pure virtual function with the same name, but different, non-covariant return types, how can I derive from these and define an implementation for both their functions?
#include <iostream>
class ITestA {
public:
virtual ~ITestA() {};
virtual float test() =0;
};
class ITestB {
public:
virtual ~ITestB() {};
virtual bool test() =0;
};
class C : public ITestA, public ITestB {
public:
/* Somehow implement ITestA::test and ITestB::test */
};
int main() {
ITestA *a = new C();
std::cout << a->test() << std::endl; // should print a float, like "3.14"
ITestB *b = dynamic_cast<ITestB *>(a);
if (b) {
std::cout << b->test() << std::endl; // should print "1" or "0"
}
delete(a);
return 0;
}
只要我不直接调用C :: test没有什么模糊,所以我认为它应该工作在某种方式,我想我只是没有找到正确的符号。或者这是不可能的,如果是这样的话:为什么?
As long as I don't call C::test() directly there's nothing ambiguous, so I think that it should work somehow and I guess I just didn't find the right notation yet. Or is this impossible, if so: Why?
推荐答案
好吧,这是可能的,方式不是太丑陋。我必须添加额外的继承:
Okay, it is possible, and the way isn't too ugly. I have to add an additional level of inheritance:
ITestA ITestB <-- These are the interfaces C has to fulfill, both with test()
| |
ITestA_X ITestB_X <-- These classes implement the interface by calling a
| | function with a different name, like ITestA_test
\__________/ which is in turn pure virtual again.
|
C <-- C implements the new interfaces
现在C没有函数 test()
,但是当向 ITestA *
C * >,将使用 ITestA_test
中的 test()
实现。当将它转换为 ITestB *
时,即使通过 ITestA *
的dynamic_cast, ITestB_test
。
以下程序打印:
3.14
0
Now C has no function test()
, but when casting a C*
to an ITestA*
, the implementation of test()
in ITestA_test
will be used. When casting it to an ITestB*
, even by a dynamic_cast from the ITestA*
, the implementation of ITestB_test
will be used.
The following program prints:
3.14
0
#include <iostream>
class ITestA {
public:
virtual ~ITestA() {};
virtual float test() =0;
};
class ITestB {
public:
virtual ~ITestB() {};
virtual bool test() =0;
};
class ITestA_X : public ITestA {
protected:
virtual float ITestA_test() =0;
virtual float test() {
return ITestA_test();
}
};
class ITestB_X : public ITestB {
protected:
virtual bool ITestB_test() =0;
virtual bool test() {
return ITestB_test();
}
};
class C : public ITestA_X, public ITestB_X {
private:
virtual float ITestA_test() {
return 3.14;
}
virtual bool ITestB_test() {
return false;
}
};
int main() {
ITestA *a = new C();
std::cout << a->test() << std::endl;
ITestB *b = dynamic_cast<ITestB *>(a);
if (b) {
std::cout << b->test() << std::endl;
}
delete(a);
return 0;
}
这有没有你能想到的缺点?
Does this have any drawbacks you could think of?
这篇关于实现两个函数具有相同的名称,但不同的非协变返回类型由于多个抽象基类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!