Why don't STL containers have virtual destructors?

后端 未结 9 2196
时光取名叫无心
时光取名叫无心 2020-11-28 07:54

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

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

  • it reduces the size of an insta
9条回答
  •  情深已故
    2020-11-28 08:04

    As has been pointed out, the STL containers are not designed to be inheritable. No virtual methods, all data members are private, no protected getters/setters/helpers.. And as you've discovered, no virtual destructors..

    I'd suggest you should really be using the containers via composition rather than implementation inheritance, in a "has-a" way rather than an "is-a" one.

提交回复
热议问题