Casting custom shared_ptr object I am using custom shared_ptr class for some program on Arduino, and I need to do something like this:

shared_ptr&lt;Base_class&gt; b_ptr;
shared_ptr&lt;Child_class&gt; ptr((shared_ptr&lt;Child_class&gt;) b_ptr);


In my shared_ptr class I have three constructors:

shared_ptr() : pData(0), reference(0){...}
shared_ptr(T* pValue) : pData(pValue), reference(0){...}
shared_ptr(const shared_ptr&lt;T&gt;&amp; sp) : pData(sp.pData), reference(sp.reference){...}


and some other stuff...
For casting I tried to write the following:

    template&lt; typename T2 &gt;
operator T2 * () const
{
   return (T2 *)pData;
}


and:

    template&lt; typename T2 &gt;
operator shared_ptr&lt;T2&gt; () const
{
    shared_ptr&lt;T2&gt; temp((T2 *)pData);
    return temp;
}


but can't get it to work.
For doing:

shared_ptr&lt;Base_class&gt; b_ptr;
shared_ptr&lt;Child_class&gt; ptr(b_ptr);


you have to use another constructor. If you take a look on this Example of shared_ptr implementation, you can see:

template&lt;class U&gt; shared_ptr(const shared_ptr&lt;U&gt;&amp; s) ...


That's exactly the constructor, that takes shared_ptr of different type .

And similarly for assigment operator.