Given the code below, the compiler is showing a message pointing that error: templates may not be ‘virtual’
. Does anyone have a suggestion on how to solve the bug?
template < class FOO_TYPE>
class CFoo{
public:
...
template < class BAR_TYPE >
virtual void doSomething( const CBar<BAR_TYPE> &); // here's the error
...
virtual ~CFoo();
protected:
MyClass < FOO_TYPE > * m_pClass;
};
template < class FOO_TYPE >
template < class BAR_TYPE >
void CFoo<FOO_TYPE>::doSomething( const CBar<BAR_TYPE> & refBar ){
...
}
The easiest reason to see why this is illegal is by considering the vtable. Sure, that's just one common implementation, and others are allowed. But all
virtual
functions in C++ are designed such that they can be implemented with a vtable.Now, how many entries are there in the
vtable
ofCFoo<int>
? Is there an entry fordoSomething<float>
? AnddoSomething<float*>
? AnddoSomething<float**>
? Templates such as these allow an infinite set of functions to be generated. Usually that's no problem, as you use only a finite subset, but for virtual functions this subset isn't known, and therefore the vtable would need to be infinite.Now, it's possible that you really wanted only a single entry in the vtable. In that case, you'd write it as follows:
This means that the vtable for
CFoo<int, float>
will have one entry, fordoSomething(float const&)
.You can use what we call in Symbian as "template design pattern". Here is sample code to give you an idea:
Well, the error message is pretty clear. Member function templates can't be virtual. How to solve this depends on your problem, but the easiest thing to do would be to make the member functions non-virtual and reconsider your design.
If you really need to make this method virtual, consider making
CBar<>
polymorphic and pass a base type in which isn't templated.EDIT: something like this: