title: Is it good practice to store copies of the same shared pointers in different vectors?
body: <p>I have a base class, <code>BaseObject</code>, and two derived class <code>DerivedObject1</code> and <code>DerivedObject2</code>. They share a common behavior and methods, but <code>DerivedObject1</code> has an additional method. My main class <code>MyClass</code> stores (in <code>std::vector</code>) <code>boost::shared_ptr</code> of instances of those classes. <code>MyClass</code> needs to call <code>commonMethod()</code> for all the BaseObject, and sometimes call <code>additionalMethod()</code> for all DerivedObject1. </p>
  
  <pre><code>class BaseObject
  {
    virtual void commonMethod();
  }
  
  Class DerivedObject1 : public BaseObject
  {
    void commonMethod();
    void additionalMethod();
  }
  
  Class DerivedObject2 : public BaseObject
  {
    void commonMethod();
  }
  </code></pre>
  
  <p>Are there any disadvantages of having two vectors in <code>MyClass</code>, one that stores ALL the pointers of <code>DerivedObject1</code> and <code>DerivedObject2</code>, and another vector that stores only the pointers of <code>DerivedObject1</code> ? Meaning I would have all the <code>DerivedObject1</code> pointers twice. But I think the call to the different methods would be clear, at least.</p>
  
  <pre><code>class MyClass
  {
    typedef std::vector&lt;std::shared_ptr&lt;BaseObject&gt;&gt; BaseObjectVector;
    typedef std::vector&lt;std::shared_ptr&lt;DerivedObject1&gt;&gt; DerivedObject1Vector;
    BaseObjectVector everything;
    DerivedObject1Vector only_derived1;
  
    void doSomething()
    {
      for (BaseObjectVector::iterator iter = everything.begin(); iter != everything.end(); ++iter)
      {
        (*iter)-&gt;commonMethod();
      }
    }
  
    void doSomethingForDerivedObject1()
    {
      for (DerivedObject1Vector::iterator iter = only_derived1.begin(); iter != only_derived1.end(); ++iter)
      {
        (*iter)-&gt;additionalMethod();
      }
    }
  }
  </code></pre>
  
  <p>I can think of other ways to do this, mainly having one vector for <code>DerivedObject1</code> and one vector for <code>DerivedObject2</code>, but to call <code>commonMethod()</code>, I would have to iterate over both vectors. My original solution seems the best to me, except that some pointers are stored twice. What are the disadvantages of this ?</p>