如果我使用模板参数 T=int* 从 CBaseInterface(参见下面的代码)派生,编译器将失败并出现错误 C2555。用于 T 的所有指针类型都会发生这种情况。如果我改用 typedef,则相同的代码可以正常工作。
// If _FALIS is defined, the compiler fails, else it succeeds
// (MS Visual Studio 2013 Update 2, 32 and 64 Bit native C++, Debug build).
#define _FALIS
#ifdef _FALIS
#define PINT int*
#else
typedef int* PINT;
#endif
template <class T>
class CBaseInterface
{
public:
virtual ~CBaseInterface() {}
virtual const T Calculate() const = 0;
};
class CCalculator : public CBaseInterface<PINT>
{
public:
CCalculator() {}
virtual ~CCalculator() {}
// error C2555: 'CCalculator::Calculate':
// overriding virtual function return type differs and is not 'covariant'
// from 'CBaseInterface<int *>::Calculate'
virtual inline const PINT Calculate() const final
{
return (PINT)&m_Item;
}
protected:
int m_Item = 0;
};
指针类型的问题在哪里?我很困惑,我在 Microsoft 的文档中没有找到适合这种情况的任何内容。
希望您能够帮助我。