title: Smart pointers and dynamic_cast
body: <p>I apologize in advance if this was answered already as I looked and could not find the answer.</p>
  
  <p>NOTE: this <strong>IS</strong> a homework assignment, so if you feel uncomfortable answering, I completely understand.</p>
  
  <p>I have the following:</p>
  
  <p>ptr.h:</p>
  
  <pre><code>template&lt;typename T&gt;
  class Ptr {
      T* address;
      size_t* counter;
      Ptr(T* address) : address(address), counter(new size_t(1)) { }
      Ptr(const Ptr&amp; other) : address(other.address), counter(other.counter) 
      { 
          ++(*counter); 
      }
      virtual ~Ptr() { 
          if (0 == --(*counter)) { delete address; delete counter; } 
      }
      Ptr&amp; operator=(const Ptr&amp; right) {
          if (address != right.address) {
              if (0 == --(*counter)) { delete address; delete counter; }
              address = right.address;
              counter = right.counter;
              ++(*counter);
          }
          return *this;
      }
      T&amp; operator*()  const { TRACE(address); return *address; }
      T* operator-&gt;() const { TRACE(address); return address;  }
      T* raw()        const { TRACE(addr); return addr;  }
  };
  </code></pre>
  
  <p>main.cc:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "ptr.h"
  
  using std::cout;
  using std::endl;
  
  
  class Base {
  public:
      Base()  { std::cout &lt;&lt; "Base()" &lt;&lt; std::endl; }
      virtual ~Base() { std::cout &lt;&lt; "~Base()" &lt;&lt; std::endl; }
      virtual std::string str() { return "In Base::str()"; }
  };
  
  class Derived: public Base {
  public:
      Derived()  { std::cout &lt;&lt; "Derived()" &lt;&lt; std::endl; }
      ~Derived() { std::cout &lt;&lt; "~Derived()" &lt;&lt; std::endl; }
      std::string str() { return "In Derived::str()"; }
  };
  
  
  
  int main() {
  Ptr&lt;Base&gt; base(new Base());
  Ptr&lt;Derived&gt; derived(new Derived());
  
  Ptr&lt;Base&gt; pbase(0);
  Ptr&lt;Derived&gt; pderived(0);
  
      // upcasting can be done like this, but is it the best way?
      pbase = *((Ptr&lt;Base&gt;*)(&amp;derived));
      cout &lt;&lt; pbase-&gt;str() &lt;&lt; endl;   // outputs: "In Derived::str()"
  
      /* 
       * downcasting should be done using dynamic_casts
       * but how can I downcast here? 
       * what do I know so far:
       * 1. just because Derived is a subclass of Base does not mean Ptr&lt;Derived&gt; is a 
       * subclass of Ptr&lt;Base&gt; so there is no hierarchy between the two so I cannot 
       * really use dynamic_casts here
       * 2. The dynamic_cast I do use is sort of useless no? since pbase is a Ptr&lt;Base&gt;
       */
      pderived = *((Ptr&lt;Derived&gt;*)(dynamic_cast&lt;Ptr&lt;Base&gt;*&gt;(&amp;pbase)));
      cout &lt;&lt; pderived-&gt;str() &lt;&lt; endl;
  
  
  return 0;
  }
  </code></pre>
  
  <p>Now, the goal was to use dynamic_cast to go back and forth, and although I found many interesting tidbits about smart pointers nothing really explained <em>how</em> this is implemented.</p>
  
  <p>I tried just getting the address field for pbase, and then initializing pderived to a new Ptr with that address but of course my reference counts got all screwed up. </p>
  
  <p>I tried creating a new Ptr that held a reference to the counter of pderived but then I couldn't set the address field of pderived so I got stuck there too.</p>
  
  <p>I'm telling you guys this information because:
  1. I want to make a point that I've been working on this for quite some time before asking help online and 
  2. I want you to know what I've tried already.</p>
  
  <p>I could really use some advice here. Just how to get:</p>
  
  <pre><code>pderived = &lt;SOMETHINGSOMETHING&gt;pbase&lt;SOMETHINGSOMETHING&gt;
  </code></pre>
  
  <p>Thanks!</p>