在模板类中是否允许使用纯虚拟方法? [英] Are pure virtual methods allowed within a template class?

查看:449
本文介绍了在模板类中是否允许使用纯虚拟方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前,我确定你不能这样做,但有一天我正在玩一些代码,它似乎编译和工作。我只是想验证,我不只是幸运。模板类可以有一个纯虚函数 - 我想这也意味着只有纯虚方法对析构函数也是有效的。

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.

但是,当涉及模板类的虚拟成员函数时,它似乎有所不同,因为当模板类变量被实例化时,整个类本身通过template参数被创建。在这一点上,由于模板的查找和替换性质,虚拟方法就像类的任何其他虚拟方法一样。

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.

无论如何,问题再次出现,以防它在那里丢失:
在tempate类中是否允许虚拟函数(纯和/或正常)?

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?

推荐答案

类模板确实可以包含虚拟或纯虚拟函数。这是Andrei Alexandresu在现代C ++设计中使用的,以使用模板和类型列表实现访问者模式。您可以在其Loki库中查看代码这里的<

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 library if you're interested.

对于大多数标准C ++实现,这是很好的,因为当模板被实例化时,虚函数最终只是一个单一的函数。因此,vtable中所需的槽的数量可以在翻译单元内是已知的,因此可以生成vtable。

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.

如前所述,您不能有虚拟模板成员函数,因为在转换单元内不会知道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.

希望这有助于!

这篇关于在模板类中是否允许使用纯虚拟方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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