title: delete via a pointer to Derived, not Base
body: <p>I implemented a basic Smart pointer class. It works for the following type of code.
  (considering Base1 has a public constructor)</p>
  
  <pre><code>Sptr&lt;Base1&gt; b(new Base1);
  b-&gt;myFunc();
  {
      Sptr&lt;Base1&gt; c = b;
      Sptr&lt;Base1&gt; d(b);
      Sptr&lt;Base1&gt; e;
      e = b;
  }
  </code></pre>
  
  <p>But in the test code it has a protected constructor(I need it to be this way). and the code</p>
  
  <pre><code>Sptr&lt;Base1&gt; sp(new Derived);
  </code></pre>
  
  <p>Produces the following error (notice the Derived):</p>
  
  <pre><code>Sptr.cpp: In instantiation of ‘my::Sptr&lt;T&gt;::~Sptr() [with T = Base1]’:
  Sptr.cpp:254:39:   required from here
  Sptr.cpp:205:9: error: ‘Base1::~Base1()’ is protected
  Sptr.cpp:97:17: error: within this context
  </code></pre>
  
  <p>The problem is I have to Make sure that you delete via a pointer to Derived, not Base1. How can I do that?</p>
  
  <p>Here is the class code (clipped to show constructor and distructor and class members)</p>
  
  <pre><code>template &lt;class T&gt;
  class Sptr {
  private:
     T* obj; // The actual object pointed by
         RC* ref;// A reference object to keep track of count
  public:
    //declarations
  
  
  template &lt;typename T&gt;
  Sptr&lt;T&gt;::Sptr():obj(NULL),ref(NULL) {
      //do something
      std::cout&lt;&lt;"()\n";
      ref = new RC();
      ref-&gt;AddRef();
  }
  
  template &lt;typename T&gt;
  Sptr&lt;T&gt;::Sptr(const Sptr &amp;a) : obj(a.obj),ref(a.ref) {
      //do something
      std::cout&lt;&lt;"const Sptr\n";
      ref-&gt;AddRef();
  }
  
  template &lt;typename T&gt;
  Sptr&lt;T&gt;::~Sptr() {
      //do something
      if(ref-&gt;Release() == 0) {
          if(obj)
              delete obj;
  
          delete ref;
      }
  }
  
  template &lt;typename T&gt;
  template &lt;typename U&gt;
  Sptr&lt;T&gt;::Sptr(U* u) : obj(u),ref(NULL) {
      //do something
      ref = new RC();
      ref-&gt;AddRef();
  }
  
  template &lt;typename T&gt;
  template &lt;typename U&gt; 
  Sptr&lt;T&gt;::Sptr(const Sptr&lt;U&gt; &amp;u) : obj(u.obj),ref(u.ref) {
      std::cout&lt;&lt;"const Sptr&lt;U&gt;\n";
      ref-&gt;AddRef();
  }
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>The destructor is not virtual. That is the case I have to solve. Below is the <code>Base1</code> and <code>Derived</code> classes</p>
  
  <pre><code>class Base1 {
      protected:
          Base1() : derived_destructor_called(false) {
              printf("Base1::Base1()\n");
          }
      private:
          Base1(const Base1 &amp;); // Disallow.
          Base1 &amp;operator=(const Base1 &amp;); // Disallow.
      protected:
          ~Base1() {
              printf("Base1::~Base1()\n");
              assert(derived_destructor_called);
          }
      protected:
          bool derived_destructor_called;
  };
  
  class Derived : public Base1 {
          friend void basic_tests_1();
      private:
          Derived() {}
          Derived(const Derived &amp;); // Disallow.
          Derived &amp;operator=(const Derived &amp;); // Disallow.
      public:
          ~Derived() {
              printf("Derived::~Derived()\n");
              derived_destructor_called = true;
          }
          int value;
  };
  </code></pre>