Override virtual method using template method

Possible duplicate:
Can a member function template be virtual?

In the base class, the my_func function my_func defined as virtual. However, in a derived class, I would like to have my_func as a template method. Is it possible?

This does not seem to be the case. I get an error "cannot select object of abstract type", which, I believe, is due to the fact that the compiler does not recognize the override of virtual my_func in the base class. Could this reveal a bad design?

Thank you very much.

UPDATE: Thanks for the answers. Some of you suggest that I should post some of the code, so here it is. In the base class:

 virtual void Fill(EventInfo* info, EasyChain* tree, vector<Muon*>& muons, vector<Electron*>& electrons, vector<Jet*>& jets, LorentzM& met) = 0; 

But in a derived class, I would like to have:

 template<typename _Jet> void Fill(EventInfo* info, EasyChain* tree, vector<Muon*>& muons_in, vector<Electron*>& electrons_in, vector<_Jet>& jets_in, LorentzM& met){ 

From your answers, I understand that the solution to the problem is to define another function in the derived class:

 void Fill(EventInfo* info, EasyChain* tree, vector<Muon*>& muons, vector<Electron*>& electrons, vector<Jet*>& jets, LorentzM& met){ // } 

but then this function and the template function are the same for the case of _Jet being Jet* , would this not be a problem?

Some of them suggested a design problem here, I think it's true, I will have to think about how to get around this.

+7
source share
4 answers

Your template method overloads the original (same name, but different parameters). You still have to override the original to make your derived class non-abstract. You can do without problems, so you will have two versions of the method in the derived class, just be careful and realize which one will be called ...

You can then make an overridden version of the overload method to invoke a new version of the template overload. This may or may not do what you want to achieve, depending on what you want to achieve ...

It might be better for the template method to have a different name to avoid confusion, because you cannot call it directly unless you have a pointer to the type of the derived class. If you have a pointer to an abstract base class, you must call the method with the parameters defined there, even if it is a virtual method, and the method of the derived class is what is actually called.

+5
source

The problem is that the template changes the signature of the function, so it no longer overrides the virtual function in the base class, so the class continues to be abstract.

Virtual function templates seem to defeat the polymorphic nature of the virtual function in the base class.

+2
source

A function in a derived class must have the same signature in order to correctly override the function of your base class (and get rid of an abstract type error). It means:

  • name of the same name
  • same numbers and types of parameters
  • the same qualifiers (e.g. constness)
  • compatible return types (even if it is not technically part of the iirc signature)

Thus, using the template in this case can lead to such an error. It would be best to place a sample code so that people can better understand your specific case.

+1
source

You cannot do this because the version of the my_func template my_func not covariant with the base class. His design problem you have here, by the way.

+1
source

All Articles