STL的继承

Posted xiang-yin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了STL的继承相关的知识,希望对你有一定的参考价值。

网上的观点都不赞成继承STL.

SO上的一篇问答:

Is it okay to inherit implementation from STL containers, rather than delegate?

The risk is deallocating through a pointer to the base class (delete, delete[], and potentially other deallocation methods). Since these classes (deque, map, string, etc.) don‘t have virtual dtors, it‘s impossible to clean them up properly with only a pointer to those classes:

struct BadExample : vector<int> {};
int main() {
  vector<int>* p = new BadExample();
  delete p; // this is Undefined Behavior
  return 0;
}

That said, if you‘re willing to make sure you never accidentally do this, there‘s little major drawback to inheriting them—but in some cases that‘s a big if. Other drawbacks include clashing with implementation specifics and extensions (some of which may not use reserved identifiers) and dealing with bloated interfaces (string in particular). However, inheritance is intended in some cases, as container adapters like stack have a protected member c (the underlying container they adapt), and it‘s almost only accessible from a derived class instance.

Instead of either inheritance or composition, consider writing free functions which take either an iterator pair or a container reference, and operate on that. Practically all of <algorithm> is an example of this; and make_heap, pop_heap, and push_heap, in particular, are an example of using free functions instead of a domain-specific container.

So, use the container classes for your data types, and still call the free functions for your domain-specific logic. But you can still achieve some modularity using a typedef, which allows you to both simplify declaring them and provides a single point if part of them needs to change:

typedef std::deque<int, MyAllocator> Example;
// ...
Example c (42);
example_algorithm(c);
example_algorithm2(c.begin() + 5, c.end() - 5);
Example::iterator i; // nested types are especially easier

Notice the value_type and allocator can change without affecting later code using the typedef, and even the container can change from a deque to a vector.

以上是关于STL的继承的主要内容,如果未能解决你的问题,请参考以下文章

STL的继承

如何从 STL 类“继承”迭代器?

比给定值最小的最大元素的 STL 算法

STL容器自定义内存分配器

STL容器自定义内存分配器

STL容器自定义内存分配器