title: Inheritance on C++ Classes that contain another Derived classes
body: <p>Having the next C++ classes as a simplifiacation of the issue:</p>
  
  <pre><code>struct Car
  {
      virtual int get_price() = 0;
  };
  
  struct ExpensiveCar: public Car
  {
      int get_price( ) {/*..*/ }
      void apply_turbo( ){/*..*/};
  };
  
  struct CheapCar: public Car
  {
      int get_price( ) {/*..*/}
  };
  
  struct CarRetailer
  {
      virtual std::vector&lt;Car*&gt; get_cars( ) = 0;
  };
  
  
  struct ExpensiveCarsRetailer : public CarRetailer
  {
      virtual std::vector&lt;Car*&gt; get_cars( ) { /*..*/ }
      std::vector&lt;ExpensiveCar*&gt; get_cars_for_exhibitions( );
  };
  
  struct CheapCarsRetailer : public CarRetailer
  {
      virtual std::vector&lt;Car*&gt; get_cars( ) { /*..*/ }
      std::vector&lt;CheapCar*&gt; get_second_hand_cars( );
  };
  </code></pre>
  
  <p>The rules are: Expensive Cars are only sold in ExpensiveCarsRetailers (similar for Cheap Cars ). Cheap cars do not have turbo and expensive cars are not sold in second hand.</p>
  
  <p>The issue I face here is the coupling of classes that contains inherited classes as well. Therefore, if <code>ExpensiveCarRetailer</code> inherits from <code>CarRetailer</code>, it will need to implement 
  <code>virtual std::vector&lt;Car*&gt; get_cars( )</code> that is actually returning a vector of <code>Car*</code>, however, internally <code>ExpensiveCarRetailer</code> only created objects of <code>ExpensiveCar</code>. Furthermore, the <code>get_cars_for_exhibitions()</code> is not included in the public interface <code>CarRetailer</code>therefore, it can return a <code>std::vector&lt;ExpensiveCar*&gt;</code> instead.</p>
  
  <p>The mixture in the API ( returning vector of <code>Car*</code> and <code>ExpensiveCar*</code> ) is very ugly, and so is the code that the user needs to write to use the <code>apply_turbo( )</code> function of the list of cars from a specific <code>ExpesiveCarsRetailer</code>.</p>
  
  <pre><code>ExpensiveCarsRetailer ferrari;
  
  std::vector&lt;Car*&gt; car = ferrari.get_cars();
  ExpensiveCar* expensive_car;
  for( int i = 0; i &lt; car.size( ); ++i)
  {
  expensive_car = dynamic_cast&lt;ExpensiveCar*&gt;(car[i]);
  expensive_car-&gt;apply_turbo();
  }
  </code></pre>
  
  <p>I am sure I am missing some design pattern that helps in this situation, where to trees of class inheritances are coupled in a way that the abstract class of one of the inheritance tress need to return a vector (or set, list, etc ) of classes on the other inheritance tree. I am trying to avoid the dynamic casting as much as possible.</p>
  
  <p>I also thought about making <code>CarRetailer</code> a templated class, therefore:</p>
  
  <pre><code>template&lt;typename T&gt;
      struct CarRetailer
      {
          virtual std::vector&lt;T*&gt; get_cars( ) = 0;
      };
  </code></pre>
  
  <p>And then make:</p>
  
  <pre><code>struct ExpensiveCarRetailer: public CarRetailer&lt;ExpensiveCar&gt;
  {
  ...
  }
  </code></pre>
  
  <p>But I don't think this would work since, for example, the <code>CarRetailer</code> can also start selling motorbikes ( similar structure as cars ) or bikes etc ( always applying the Expensive/Cheap pattern ) and eventually the number of template classes that need to be defined in <code>CarRetailer</code> will be huge.</p>