用模板方法覆盖虚拟方法 [英] override virtual method with template method

查看:63
本文介绍了用模板方法覆盖虚拟方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
成员函数模板可以是虚拟的吗?

Possible Duplicate:
Can a member function template be virtual?

在基类中,功能my_func被定义为虚函数.但是,在派生类中,我希望使用my_func作为模板方法.这可能吗?

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?

似乎不是.我收到错误无法分配抽象类型的对象" ,我认为这与以下事实有关:编译器未确认基类中对虚拟my_func的覆盖.这是否表明设计不佳?

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?

非常感谢.

更新: 感谢您的回答.有人建议我应该发布一些代码,就在这里. 在基类中:

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){
//
}

但是,对于_JetJet*的情况,此函数和模板函数相同,这不是问题吗?

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.

推荐答案

您的模板化方法重载了原始名称(名称相同但参数不同).您还必须覆盖原始文件,以使派生类不抽象.两者都可以做,所以在派生类中将有两种版本的方法,请小心并意识到哪个将被调用...

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.

这篇关于用模板方法覆盖虚拟方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆