我有一个通用的IDetachable接口,其中提供了一个方法:
template<class T>
class IDetachable {
public:
virtual T detached() const = 0;
};
我有这些示例类:
class A: public IDetachable<A> {
virtual A detached() const override {
// some implementation which returns a detached A object
}
};
问题出现在继承 B 时:
class B: public A, public IDetachable<B> {
virtual B detached() const override {
// some implementation which returns a detached B object
}
};
clang++告诉我:
error: virtual function 'detached' has a different return type ('B')
than the function it overrides (which has return type 'A')
对我来说,编译器似乎自动选择了第一个父类来使用override关键字。有没有可能告诉编译器我想要继承哪个父类的方法呢?
override
的作用 - 如果派生类中函数的签名与基类中的函数不完全相同,它将导致错误。 - Jonathan PotterIDetachable<B>
中的detached
而不是A
。 - T.C.B
拥有两个detached()
函数——一个返回A
,另一个返回B
——还是只有一个这样的函数? - seh