为什么STL容器不具有虚拟析构函数? [英] Why don't STL containers have virtual destructors?

查看:176
本文介绍了为什么STL容器不具有虚拟析构函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道为什么STL容器没有虚拟析构函数?

Does anyone know why the STL containers don't have virtual destructors?

据我所知,唯一的好处是:

As far as I can tell, the only benefits are:


  • 通过一个指针(对于虚拟方法表)和

  • 减少一个实例的大小,这使得破坏和构建速度快一点。

缺点是以通常的方式子类化容器是不安全的。

The downside is that it's unsafe to subclass the containers in the usual way.

编辑:
也许我的问题可以改写为为什么STL容器设计不允许继承?

Perhaps my question could be rephrased "Why weren't STL containers designed to allow for inheritance?"

因为它们不支持继承,当需要一个需要STL功能的新容器和少量附加功能时,会遇到以下选择(例如一个专门的构造函数或者具有默认值的新访问器,或者其他):

Because they don't support inheritance, one is stuck with the following choices when one wants to have a new container that needs the STL functionality plus a small number of additional features (say a specialized constructor or new accessors with default values for a map, or whatever):


  • 组合和接口复制:创建一个拥有STL容器的新模板或类作为私有成员,并为每个STL方法有一个直通内联方法。这与继承一样有效,避免了虚拟方法表的成本(在重要的情况下)。不幸的是,STL容器具有相当宽泛的接口,所以这需要很多行代码看起来很容易做。

  • 只是做功能:use bare (可能模板化)文件范围的函数,而不是试图添加成员函数。

  • 具有公共STL访问的组合:让STL容器的所有者允许用户访问STL容器本身(可能通过访问器保护)。这需要对库编写器进行最少的编码,但对于用户来说这种方法更不方便。组合的一个主要卖点是减少代码中的耦合,但是这个解决方案将STL容器与所有者容器完全耦合(因为所有者返回一个真正的STL容器)。

  • 编译时多态性:写作有些棘手,需要一些代码体操,不适合所有情况。

  • Composition and interface replication: Make a new template or class that owns the STL container as a private member and has one pass-through inline method for each STL method. This is just as performant as inheritance, avoids the cost of a virtual method table (in the cases where that matters). Unfortunately, the STL containers have fairly broad interfaces so this requires many lines of code for something that should seemingly be easy to do.
  • Just make functions: Use bare (possibly templated) file-scoped functions instead of trying to add member functions. In some ways this can be a good approach, but the benefits of encapsulation are lost.
  • Composition with public STL access: Have the owner of the STL container let users access the STL container itself (perhaps guarded through accessors). This requires the least coding for the library writer, but it's much less convenient for users. One of the big selling points for composition is that you reduce coupling in your code, but this solution fully couples the STL container with the owner container (because the owner returns a true STL container).
  • Compile-time polymorphism: Can be somewhat tricky to do write, requires some code gymnastics, and isn't appropriate for all situations.

作为一个侧面的问题:有一个标准安全的子类化与非虚拟析构函数的方法(让我们假设我不想覆盖任何方法,只是我想添加新的) ?我的印象是,如果没有能力改变定义非虚拟类的代码,就没有通用和安全的方法。

As a side question: is there a standards-safe way of subclassing with non-virtual destructors (let's assume that I don't want to override any methods, just that I want to add new ones)? My impression is that there is no generic and safe way of doing this if one does not have the power to change the code defining the non-virtual class.

EDIT 2:

由于 @doc指出

As @doc points out, C++ 11's fancier using declarations lower the cost of composition somewhat.

推荐答案

p>我猜它遵循C ++的哲学,不支付你不使用的功能。根据平台,如果你不关心虚拟析构函数,虚拟表的指针可能是一个很高的价格。

I guess it follows the C++ philosophy of not paying for features that you don't use. Depending on the platform, a pointer for the virtual table could be a hefty price to pay if you don't care about having a virtual destructor.

这篇关于为什么STL容器不具有虚拟析构函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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