title: How should smart pointers get down casted?
body: <p>Do smart pointers handle down casting, and if not what is a safe way of working around this limitation?</p>
  
  <p>An example of what I'm trying to do is having two STL vectors (for example) containing smart pointers.  The first contains smart pointers to a base class while the second contains smart pointers to a derived class.  The smart pointers are referenced counted, e.g. similar behaviour to Boost's shared_ptrs, but hand-rolled.  I've included some sample code that I whipped up to provide an example:</p>
  
  <pre><code>vector&lt;CBaseSmartPtr&gt; vecBase;
  vector&lt;CDerivedSmartPtr&gt; vecDer;
  ...
  CBaseSmartPtr first = vecBase.front();
  vecDer.push_back(CDerivedSmartPtr(dynamic_cast&lt;CDerived*&gt;(first.get()));
  </code></pre>
  
  <p>This seems <strong>not</strong> safe to me, as I think I'm ending up with two smart pointers managing the same object.  At some point down the track this is probably going to result in one of them freeing the object while the other still holds references to it.</p>
  
  <p>What I'd hope for but don't think will work is a straight down-cast while keeping the same object, e.g. </p>
  
  <pre><code>dynamic_cast&lt;CDerivedSmartPtr&gt;(first)
  </code></pre>
  
  <p>Should I be looking to change the second container to also use CBaseSmartPtr and downcast on usage only?  Are there other solutions?</p>