我有這相當於代碼這一個:C++多重繼承協變返回類型錯誤
class X {};
class Y {};
template< typename T>
class C {
public:
virtual T * foo() = 0;
};
class A : public C< X> {
public:
X * foo() {};
};
class B : public A {};
class D : public B, public C< Y> {
public:
Y * foo() {}; //this is the only one method I need here. Not A::foo!
};
我得到這個錯誤:
error: invalid covariant return type for 'virtual Y* D::foo()'
Y * foo() {};
^
和:
error: overriding 'virtual X* A::foo()'
X * foo() {};
^
我相信我可以在B類或D以防止A :: foo從繼承,但我不知道是什麼。也許有一些功能可以重命名C++中的衝突名稱?
PS>我不能使用C++ 11,只能使用舊的C++ 98。
如果從'C'繼承的'foo'是你想要的唯一一個,那你爲什麼要從'B'繼承?這似乎是一個潛在設計問題的症狀。 –
2015-02-24 13:45:37
B和D是我在代碼中分別使用的兩個類。 – 2015-02-24 14:17:02
我已經問過一個類似於這個問題:http://stackoverflow.com/questions/28240936/whats-the-best-way-to-implement-ast-using-visitor-pattern-with-return-value,the第二個答案可能會提供你想要的,但它確實需要C++ 11。 – swang 2015-02-24 14:29:29