模板可能不是“虚拟的”;
根据下面的代码,编译器显示一条消息,指出错误:模板可能不是“虚拟”
。有人对如何解决该错误有建议吗?
template < class FOO_TYPE>
class CFoo{
public:
...
template < class BAR_TYPE >
virtual void doSomething( const CBar<BAR_TYPE> &); // here's the error
...
virtual ~CFoo();
protected:
MyClass < FOO_TYPE > * m_pClass;
};
template < class FOO_TYPE >
template < class BAR_TYPE >
void CFoo<FOO_TYPE>::doSomething( const CBar<BAR_TYPE> & refBar ){
...
}
Given the code below, the compiler is showing a message pointing that error: templates may not be ‘virtual’
. Does anyone have a suggestion on how to solve the bug?
template < class FOO_TYPE>
class CFoo{
public:
...
template < class BAR_TYPE >
virtual void doSomething( const CBar<BAR_TYPE> &); // here's the error
...
virtual ~CFoo();
protected:
MyClass < FOO_TYPE > * m_pClass;
};
template < class FOO_TYPE >
template < class BAR_TYPE >
void CFoo<FOO_TYPE>::doSomething( const CBar<BAR_TYPE> & refBar ){
...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
了解为什么这是非法的最简单的原因是考虑 vtable。当然,这只是一种常见的实现,其他的实现也是允许的。但是 C++ 中的所有虚拟函数都被设计为可以使用 vtable 来实现。
现在,
CFoo
的vtable
中有多少条目?是否有doSomething
的条目?还有doSomething
?还有doSomething
?诸如此类的模板允许生成无限组函数。通常这没有问题,因为您只使用有限子集,但对于虚函数来说,这个子集是未知的,因此 vtable 需要是无限的。现在,您可能确实只需要 vtable 中的一个条目。在这种情况下,您可以如下编写:
这意味着
CFoo
的 vtable 将有一个条目,即doSomething(float const&)
。The easiest reason to see why this is illegal is by considering the vtable. Sure, that's just one common implementation, and others are allowed. But all
virtual
functions in C++ are designed such that they can be implemented with a vtable.Now, how many entries are there in the
vtable
ofCFoo<int>
? Is there an entry fordoSomething<float>
? AnddoSomething<float*>
? AnddoSomething<float**>
? Templates such as these allow an infinite set of functions to be generated. Usually that's no problem, as you use only a finite subset, but for virtual functions this subset isn't known, and therefore the vtable would need to be infinite.Now, it's possible that you really wanted only a single entry in the vtable. In that case, you'd write it as follows:
This means that the vtable for
CFoo<int, float>
will have one entry, fordoSomething(float const&)
.如果您确实需要使此方法成为虚拟方法,请考虑使
CBar<>
多态并传递未模板化的基本类型。编辑:像这样:
If you really need to make this method virtual, consider making
CBar<>
polymorphic and pass a base type in which isn't templated.EDIT: something like this:
您可以使用我们在 Symbian 中所说的“模板设计模式”。下面是示例代码,可以给您一个想法:
You can use what we call in Symbian as "template design pattern". Here is sample code to give you an idea:
嗯,错误消息非常清楚。 成员函数模板不能是虚拟的。如何解决这个问题取决于您的问题,但最简单的方法是使成员函数成为非虚拟函数并重新考虑您的设计。
Well, the error message is pretty clear. Member function templates can't be virtual. How to solve this depends on your problem, but the easiest thing to do would be to make the member functions non-virtual and reconsider your design.