As you know, you cannot have templates for virtual functions, since all virtual functions are part of the class type and must be known in advance. This excludes any simple “arbitrary redefinition”.
If this is an option, you can make part of the class template parameter:
template <typename T> class A { protected: virtual void method(T &); }; template <typename T> class B : public A<T> { virtual void method(T &);
A more attractive approach might use some dispatcher object:
struct BaseDispatcher { virtual ~BaseDispatcher() { } template <typename T> void call(T & t) { dynamic_cast<void*>(this)->method(t); } }; struct ConcreteDispatcher : BaseDispatcher { template <typename T> void method(T &); }; class A { public: explicit A(BaseDispatcher * p = 0) : p_disp(p == 0 ? new BaseDispatcher : p) { } virtual ~A() { delete p_disp; }; private: BaseDispatcher * p_disp; template <typename T> void method(T & t) { p_disp->call(t); } }; class B : public A { public: B() : A(new ConcreteDispatcher) { }
source share