jeudi 28 juillet 2016

Reimplement a virtual function from base class in a derived template class

I have a base class which has a virtual function :

class Base {
   ...
   virtual void myFunction(){assert(0 && "not implemented yet");}

}

and a derived (template) class of Base :

DerviedClass.hpp :

Template<typename T>
class DerivedClass : public Base, public T{

  ...
  void myFunction();

}

DerivedClass.cpp :

template<>
void DerivedClass<ClassA>::myFunction(){
 //Something ClassA is suppose to do
}

This compiles. But when I try to instanciate a DerivedClass<ClassB> I get the error :

IProject.o:-1: erreur : undefined reference to `DerivedClass<ClassB>::myFunction()'

Why do I have this error ? Why it does not take Base::myFunction() instead of forcing me to implement a generic myFunction in DerivedClass or a specialized function DerivedClass::myFunction() ?

Note : the assert in myFunction is because ClassB is not supposed to call myFunction during runtime. For exemple if myFunction is getRadius, DerivedClass<Circle>::getRadius() is okay but DerivedClass<Square>::getRadius() should not be called.

Note 2 : The other topics I found were not clear about this point

Aucun commentaire:

Enregistrer un commentaire