BUG: C2555 with Virtual Function Returning Template ObjectID: Q184089
|
A virtual function returning a templated class object and a derived class
defining the virtual function, causes the following compiler error:
error C2555: 'MyDerived::TestFunc' : overriding virtual function differs from 'MyBase::TestFunc' only by return type or calling convention
Define the base class function to be nonvirtual.
Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article.
template <class T>
class TestClass
{
public:
T val;
TestClass(T num){val=num;}
};
class MyBase
{
public:
int value;
virtual TestClass<float> TestFunc(void) ;
/* Make this function nonvirtual to fix C2555 error. */
/* A pure virtual function also has the same problem. */
};
template <class T>
class MyDerived : public MyBase
{
public:
virtual TestClass<float> TestFunc(void)
{
return TestClass<float>(3.14f);
}
};
MyDerived<int> x;
void main(void)
{
x.TestFunc();
}
(c) Microsoft Corporation 1998, All Rights Reserved. Contributions by Mark Hagen, Microsoft Corporation.
Additional query words: CL C2555 CPP CXX
Keywords : kbCompiler kbCPPonly kbLangCPP kbVC kbVC400bug kbVC410bug kbVC420bug kbVC500bug kbVC600bug
Version : winnt:4.0,4.0a,4.1,4.2,5.0,6.0
Platform : winnt
Issue type : kbbug
Last Reviewed: February 15, 1999