override virtual method with template method [duplicate]
Asked Answered
D

4

9

Possible Duplicate:
Can a member function template be virtual?

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

It seems it isn't. I get the error "cannot allocate an object of abstract type", which I believe it is related to the fact that the compiler does not acknowledge the override of the virtual my_func in the base class. Does this reveal a poor design maybe?

Thanks a lot.

UPDATE: Thanks for the answers. Some of you are suggesting 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 the 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 a solution to the problem would be 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*, wouldn't that be a problem as well?

Some have suggested a design problem here, I guess that's true, I'll have to think about how to go around this then.

Diley answered 11/12, 2012 at 10:37 Comment(6)
No, you can't do this directly. Why don't you show us some code illustrating what you're hoping to achieve with a templated override?Peppers
template method cannot be virtual, so it can never override any virtual method.Glottalized
@NPE, I added some code now.Diley
just to say, you seem to be using C++. Coding in D, I strongly advice you to use the override keyword, present in C++11 if I say well. There’re only advantages to do so, no drawback ;)Saida
@skp, I can't code in anything other than C++ for this.Diley
@elelias, I understand, it’s a pity thenSaida
A
5

Your templated method overloads the original (same name but different parameters). You still have to override the original as well, to make your derived class non-abstract. You can do both no problem, so you will have two versions of the method in the derived class, just be careful and conscious of which one will get called...

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

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

Advisedly answered 11/12, 2012 at 10:47 Comment(2)
Thanks for the answer, but how can I know which one will be called? I mean, if you look at the code I have added, now the template and the non-template function will be the same if the typename is Jet*. If, in the program, I call the function with Jet*, which one will be called? Thanks again.Diley
The most specific one is called. But, I actually can't think of a way to call the template version with same parameters from the overriden base class method... You may have to add private/protected helper method there with different name, so you'd have 3 methods: one-liner override of abstract Fill(), one-liner templated overload of Fill(), protected Fill2() with identical parameters to the templated overload, and would call that from both versions of Fill().Advisedly
G
2

The problem is that the template is changing the signature of the function, so it's no longer overriding the virtual function in the base class, therefore you class continues to be abstract.

Templating a virtual function seems to defeat the polymorphic nature of the virtual function in the base class.

Groovy answered 11/12, 2012 at 10:42 Comment(0)
G
1

The function in the derived class needs to have the same signature in order to properly override the function of your base class (and get rid of the abstract type error). That means :

  • same name
  • same parameter number and types
  • same qualifiers (constness for example)
  • compatible return types (even though that's technically not part of the signature iirc)

So indeed, using template in that case can lead to that kind of error. The best would be to post a code sample so people could get a better idea of your specific case.

Girovard answered 11/12, 2012 at 10:41 Comment(1)
thanks for the answer, I added some code now.Diley
S
1

You can’t do that because the my_func template version is not covariant with the base class one. It’s a design problem you have here, btw.

Saida answered 11/12, 2012 at 10:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.