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?<
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
of CFoo
? Is there an entry for doSomething
? And doSomething
? And doSomething
? 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:
template < class FOO_TYPE, class BAR_TYPE>
class CFoo{
public:
...
virtual void doSomething( const CBar &); // now OK.
...
virtual ~CFoo();
protected:
MyClass < FOO_TYPE > * m_pClass;
};
This means that the vtable for CFoo
will have one entry, for doSomething(float const&)
.