Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>The risk is deallocating through a pointer to the base class</strong> (<em>delete</em>, <em>delete[]</em>, and potentially other deallocation methods). Since these classes (<em>deque</em>, <em>map</em>, <em>string</em>, etc.) don't have virtual dtors, it's impossible to clean them up properly with only a pointer to those classes:</p> <pre><code>struct BadExample : vector&lt;int&gt; {}; int main() { vector&lt;int&gt;* p = new BadExample(); delete p; // this is Undefined Behavior return 0; } </code></pre> <p>That said, <em>if</em> you're willing to make sure you never accidentally do this, there's little major drawback to inheriting them&mdash;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 (<em>string</em> in particular). However, inheritance is intended in some cases, as container adapters like <em>stack</em> have a protected member <em>c</em> (the underlying container they adapt), and it's almost only accessible from a derived class instance.</p> <p>Instead of either inheritance or composition, <strong>consider writing free functions</strong> which take either an iterator pair or a container reference, and operate on that. Practically all of &lt;algorithm&gt; is an example of this; and <em>make_heap</em>, <em>pop_heap</em>, and <em>push_heap</em>, in particular, are an example of using free functions instead of a domain-specific container.</p> <p>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:</p> <pre><code>typedef std::deque&lt;int, MyAllocator&gt; Example; // ... Example c (42); example_algorithm(c); example_algorithm2(c.begin() + 5, c.end() - 5); Example::iterator i; // nested types are especially easier </code></pre> <p>Notice the value_type and allocator can change without affecting later code using the typedef, and even the container can change from a <em>deque</em> to a <em>vector</em>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload