smart pointer implementation for derived to base pointers
<p>I have created a smart pointer implementation as given below ::</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;vector&gt;
using namespace std;
class Obj {
   int i, j;
public:
   Obj() { i = 0 ; j = 0 ; }
   Obj(int ii , int jj) : i(ii) , j(jj) { }
   void f() { cout &lt;&lt; i &lt;&lt; endl; }
   void g() { cout &lt;&lt; j &lt;&lt; endl; }
};

class ObjDerived : public Obj
{
    int k;
    public:
    ObjDerived(int kk = 0) :k(kk) { }
    void h() { cout &lt;&lt; k &lt;&lt; endl; }
};


template &lt;typename ULT &gt;
class SP
{
       ULT* ptr;

    public:
       explicit SP(ULT* tptr = NULL) : ptr(tptr) { }
       template &lt;typename OTHER&gt;
       SP(SP&lt;OTHER&gt;&amp; other)
       {
           ptr = (ULT*)other.ptr;
       }    

       ULT* operator-&gt;() { return ptr; }
       ULT&amp; operator*() { return *ptr; }
       SP&lt;ULT&gt;&amp; operator=(SP&lt;ULT&gt;&amp; tptr)
       {
           if(ptr != tptr.ptr)
               ptr = tptr.ptr;
           return *this;
       }
       SP&lt;ULT&gt;&amp; operator=(ULT* tptr)
       {
           ptr = tptr;
           return *this;
       }

       template &lt;typename OTHER&gt;
       SP&lt;ULT&gt;&amp; operator=(SP&lt;OTHER&gt;&amp; der) // ??
       {
           cout &lt;&lt; "In operator\n";
           this-&gt;ptr = (ULT*)der.ptr;
           return *this;
       }

       ~SP() 
       { 
           if(ptr != NULL )
               delete ptr; 
       }
};


int main() 
{
    SP&lt;Obj&gt; Sptr2(new Obj(10,20));

    SP&lt;ObjDerived&gt; Sptr4(new ObjDerived(80));
    Sptr2 = Sptr4; //error in this line
    return 0;


}
</code></pre>

<p>I am trying to cast a derived class pointer to base class pointer using smart pointer SP . The operator= member function </p>

<pre><code>template &lt;typename OTHER&gt;
SP&lt;ULT&gt;&amp; operator=(SP&lt;OTHER&gt;&amp; der) // ??
{
     cout &lt;&lt; "In operator\n";
     this-&gt;ptr = (ULT*)der.ptr;
     return *this;
}
</code></pre>

<p>is giving the following error ---> error: 'ObjDerived* SP::ptr' is private</p>

<p>I am not able to find out how to achieve the desired pointer conversion using smart pointer.  I also searched previous posts on smart pointers but could not find exact answer to my problem.</p>
