title: C++ discourages base class of collection - is there anyway to fake it?
body: <p>There are no similar concept of (Java) <a href="https://stackoverflow.com/questions/20829860/what-is-the-c-equivalent-of-inheriting-a-java-collection-interface-set-map">Collection in C++</a>.      </p>
  
  <p>I can understand the reason, but I want to know whether there is any way to <strong><em>fake</em></strong> it elegantly.</p>
  
  <h2>Example</h2>
  
  <p>I have implemented many custom <code>Collection</code>s.<br>
  They all have <code>Iterator</code> that works correctly, similar to <code>std::vector</code>, <code>std::unordered_set</code>, etc.</p>
  
  <p>They are  <code>MyArray&lt;T&gt;</code> , <code>MyBinaryTree&lt;T&gt;</code> and <code>MySet&lt;T&gt;</code>.</p>
  
  <p>Here I will show a working code that show the location I want to fake it.     </p>
  
  <p>Let's say that I have 2 levels of program : library and user.<br>
  It does only one thing - <code>User</code> commands <code>Library</code> to eat all <code>Orange*</code>s in a bucket.</p>
  
  <p><strong>Library.h</strong></p>
  
  <pre><code>class Library{
      public: static void eatAll(const MyArray&lt;Orange*&gt;&amp; bucket);
  };
  </code></pre>
  
  <p><strong>Library.cpp</strong></p>
  
  <pre><code>#include "Orange.h"
  void Library::eatAll(const MyArray&lt;Orange*&gt;&amp; bucket){
      for(auto orange:bucket){
          orange-&gt;eaten();
      }
  }
  </code></pre>
  
  <p><strong>User.h</strong></p>
  
  <pre><code>MyArray&lt;Orange*&gt; bucket;
  Library::eatAll(bucket);    
  </code></pre>
  
  <p>It is OK.</p>
  
  <p>Now, I want <code>Library::eatAll</code> to also support <code>MyBinaryTree&lt;Orange*&gt;</code>, I have some  not-so-desirable approaches as below.</p>
  
  <h1>My poor solution</h1>
  
  <h2>1. Java way</h2>
  
  <ul>
  <li>Make <code>MyBinaryTree&lt;T&gt;</code> and <code>MyArray&lt;Orange*&gt;</code> (and their iterator) inherit from a new class <code>Collection&lt;T&gt;</code> (and <code>CollectionIterator&lt;T&gt;</code>).       </li>
  <li>change signature to <code>Library::eatAll(const Collection&lt;T&gt;&amp;)</code></li>
  </ul>
  
  <p><strong>Disadvantage :</strong> performance penalty from "virtual" of some functions in <code>Collection&lt;T&gt;</code>. </p>
  
  <h2>2. Template v1</h2>
  
  <pre><code>//Library.h
  template&lt;class T&gt; void eatAll(const T&amp;t ){
      for(auto orange : t){
          orange-&gt;eaten();
      }
  }
  </code></pre>
  
  <ul>
  <li>make <code>eatAll</code> a template function </li>
  </ul>
  
  <p><strong>Disadvantage :</strong>     The implementation of the <code>eatAll</code> must be in header.<br>
  I have to <code>#include orange.h</code> in <code>Library.h</code>.<br>
  Sometimes, I really want to just forward declaration.</p>
  
  <h2>3. Template v2</h2>
  
  <pre><code>//Library.h
  template&lt;class T&gt; void eatAll(const T&amp;t ){
      for(auto orange : t){
          eatAnOrange(orange)
      }
  }
  private: void eatAnOrange(Orange* orange){
      //below implementation is inside Library.cpp
      orange-&gt;eaten();
  }
  </code></pre>
  
  <ul>
  <li>create a middle-man function <code>eatAnOrange</code>     </li>
  </ul>
  
  <p><strong>Disadvantage :</strong>       </p>
  
  <ul>
  <li>Code is less readable, not concise, cause a little maintainability problem.      </li>
  <li>If there are a lot of other functions e.g. <code>squeezeAll()</code>, I probably have to create a lot of middle-man functions, e.g. <code>squeezeAnOrange()</code>. </li>
  </ul>
  
  <h2>4. Operator=()</h2>
  
  <p>Create converter among the 3 collection classes via implicit constructor.<br>
  <strong>Disadvantage :</strong>   Suffer performance from creating a new instance of collection.      </p>
  
  <pre><code>//Here is what it will do, internally (roughly speaking)
  MyBinaryTree&lt;Orange*&gt; bucket;
  Library::eatAll(MyArray&lt;Orange*&gt;(bucket)); 
  </code></pre>
  
  <p>I believe my solutions are inelegant.<br>
  Are there any solutions that don't suffer the mentioned disadvantage?    </p>
  
  <p><strong>Edit:</strong><br>
  Both of current answers are elegant than my approaches (thank!), but still has disadvantage :-<br>
  - Oliv's requires <code>#include "orange.h"</code> in <code>User.h</code>.<br>
  - Richard Hodges's has virtual function calling.</p>