C++ 模板类中是否允许使用纯虚方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8919566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Are pure virtual methods allowed within a template class?
提问by Anthony
Once before, I was certain that you couldn't do this, but the other day I was playing around with some code and it seemed to compile and work. I just want to verify that I am not just getting lucky. Can a template class have a pure virtual function - which I guess would also mean just plain virtual methods would be valid as well for the destructor?
曾经有一次,我确信你不能这样做,但前几天我在玩一些代码,它似乎可以编译和工作。我只是想验证一下,我不仅仅是走运。模板类可以有一个纯虚函数吗——我猜这也意味着只有普通的虚方法对析构函数也有效吗?
template <typename WordType> class DataSource
{
public:
DataSource();
DataSource(DataSource const& other);
virtual ~DataSource();
virtual void Put(
WordType const* const data,
unsigned int const wordCount) = 0;
}
I've tried looking it up online and all that I've been able to find is that you cannot have a virtual method (pure or otherwise) in a normal class such as this:
我试过在网上查找它,我能找到的只是在普通类中不能有虚拟方法(纯或其他),例如:
class DataSource
{
public:
DataSource();
DataSource(DataSource const& other);
virtual ~DataSource();
template <typename WordType>
virtual void Put(
WordType const* const data,
unsigned int const wordCount) = 0;
}
And that this is due to the imposibility of managing a virtual table to reference all the different types of possible types this method would be instanciated with.
这是因为无法管理虚拟表以引用此方法将使用的所有不同类型的可能类型。
However, when it came to a virtual member function of a template class, it seems to be different because the whole class itself is "created" via the template parameter when the template class variable is instanciated. At this point, the virtual method is just like any other virual method of a class due to the "find-and-replace" nature of templates.
但是,当涉及到模板类的虚拟成员函数时,它似乎有所不同,因为在实例化模板类变量时,整个类本身是通过模板参数“创建”的。在这一点上,由于模板的“查找和替换”性质,虚拟方法就像类的任何其他虚拟方法一样。
Anyway, stating the question again in case it got lost in there: Are virtual (pure and/or normal) virtual functions allowed within a tempate class?
无论如何,再次说明这个问题,以防它在那里迷路:在临时类中是否允许虚拟(纯和/或正常)虚拟函数?
回答by templatetypedef
A class template can indeed contain virtual or pure virtual functions. This was employed by Andrei Alexandresu in "Modern C++ Design" to implement the visitor pattern using templates and type lists. You can see the code here in his Loki libraryif you're interested.
类模板确实可以包含虚函数或纯虚函数。这是 Andrei Alexandresu 在“现代 C++ 设计”中使用的模板和类型列表来实现访问者模式。如果您有兴趣,可以在他的 Loki 库中查看代码。
With most standard C++ implementations, this is fine, because when the template is instantiated the virtual function ends up being one single function. Consequently, the number of slots needed in the vtable can be known within the translation unit, so a vtable can be generated.
对于大多数标准 C++ 实现,这很好,因为当模板被实例化时,虚函数最终成为一个单一的函数。因此,vtable 中所需的槽数在翻译单元内是已知的,因此可以生成 vtable。
As you mentioned, you cannot have a virtual template member function because the number of vtable slots wouldn't be known within the translation unit.
正如您所提到的,您不能拥有虚拟模板成员函数,因为在翻译单元中不知道 vtable 插槽的数量。
Hope this helps!
希望这可以帮助!
回答by jpalecek
Are virtual (pure and/or normal) virtual functions allowed within a tempate class?
模板类中是否允许使用虚拟(纯和/或普通)虚拟函数?
Yes. Perfectly legal.
是的。完全合法。
回答by JohnMcG
Think about what a template class is -- it is not a class itself, but a template the compiler can use to create classes.
想想什么是模板类——它不是一个类本身,而是一个编译器可以用来创建类的模板。
As such, there's no reason you can't include a virtual function (pure or otherwise) in the template class definition, because that, in and of itself, does not generate any code, including the virtual table.
因此,您没有理由不能在模板类定义中包含虚函数(纯函数或其他虚函数),因为它本身不会生成任何代码,包括虚拟表。
When we actually instantiate the template class, e.g. DataSource<int>
, then the compiler only needs to build the virtual table for that one selected type, so it's not any different than a (pure or otherwise) virtual function for a non-templated class.
当我们实际实例化模板类时,例如DataSource<int>
,编译器只需要为所选类型构建虚拟表,因此它与非模板类的(纯或其他)虚函数没有任何不同。
回答by Apte
A Class template with virtual functions are absolutely fine. But, template functions with virtual keyword prefixed with in a class or template class is not allowed. Below would help you get that:
带有虚函数的类模板绝对没问题。但是,不允许在类或模板类中使用以 virtual 关键字为前缀的模板函数。以下将帮助您了解:
//This is perfectly fine.
template <type T>
class myClass{
virtual void function() = 0;
};
//This is NOT OK...
template<type T>
class myClass{
template <type T>
virtual void function() = 0;
};