title: Right design pattern to deal with polymorphic collections of objects
body: <p>Suppose I have the following classes:</p>
  
  <pre><code>class BaseObject {
      public:
          virtual int getSomeCommonProperty();
  };
  
  class Object1: public BaseObject {
      public:
          virtual int getSomeCommonProperty();  // optional
          int getSomeSpecificProperty();
  };
  
  class BaseCollection {
      public:
          virtual void someCommonTask();
  };
  
  class Collection1: public BaseCollection {
      public:
          virtual void someCommonTask();  // optional
          void someSpecificTask();
  };
  </code></pre>
  
  <p>Each collection, derived from BaseCollection, deals with a specific object type (and only one type). But BaseCollection should be able to perform some tasks that are common to all objects, using only common object properties in BaseObject.</p>
  
  <p>Currently, I have potentially three solutions in mind:</p>
  
  <p>1) Store the objects list in BaseCollection, such as:</p>
  
  <pre><code>class BaseCollection {
      vector&lt;BaseObject*&gt; objects;
  };
  </code></pre>
  
  <p>The problem with this solution is that when I need to perform object-specific task in Collection1, I need a dynamic_cast&lt;>, because I don't want to use virtual inherance for specific properties, applying to only one type of object. Considering that dynamic_cast&lt;> could potentially get called millions of time per second, this seems an issue for a performance critical application.</p>
  
  <p>2) Store the objects list in Collection1, such as:</p>
  
  <pre><code>class Collection1: public BaseCollection {
      vector&lt;Object1*&gt; objects;
  }
  </code></pre>
  
  <p>But then I need some way to access this object list in BaseCollection, to be able to perform some common tasks on them, ideally through an iterator. I would need to create a function that return a vector for the BaseCollection, but again, this does not seem very efficient, because the only way to do that is to create a new vector (potentially containing thousands of objects)...</p>
  
  <p>3) Store the objects list in BaseCollection AND Collection1:</p>
  
  <pre><code>class BaseCollection {
      public:
          void someCommonTask();  // Use baseObjects
          virtual void addObject() = 0;
  
      protected:
          vector&lt;BaseObject*&gt; baseObjects;
  };
  
  class Collection1: public BaseCollection {
      vector&lt;Object1*&gt; objects;
  
      public:
          virtual void addObject() {
              Object1* obj = new Object1;
              objects.push_back(obj);
              baseObjects.push_back(obj);
          }
  
          void someSpecificTask(); // Use objects, no need of dynamic_cast&lt;&gt;
  }
  </code></pre>
  
  <p>Where the two lists actually contain the same objects. Is that as ugly as it sounds like?</p>
  
  <p>I am looking for the right/correct/best design pattern for this type of problem and none of the 3 solutions exposed above really satisfies me...</p>
  
  <p>Maybe it is possible to solve that problem with templates, but then I don't see a way to store a list of polymorphic collections like this:</p>
  
  <pre><code>vector&lt;BaseCollection*&gt; collections;
  </code></pre>