struct forward declaration and copy constructor
<p>My abstract Reference counter class:</p>

<pre><code>template&lt;class T&gt;
class ReferenceCounter
{
public:

    ReferenceCounter();
    ~ReferenceCounter();

    void addRef();
    void release();

    uint32 getCountReferences() const;

protected:
    int32* pCountReferences;

    virtual void destroyObject() = 0;
    virtual void shallowCopy(const T&amp; rhs) = 0;
};

template&lt;class T&gt;
inline ReferenceCounter&lt;T&gt;::ReferenceCounter() 
{
    pCountReferences = new int32;
    *pCountReferences = 1;
}

template&lt;class T&gt;
inline ReferenceCounter&lt;T&gt;::~ReferenceCounter() 
{
    if(pCountReferences != NULL &amp;&amp; *pCountReferences == 0)
    {
        delete pCountReferences;
        pCountReferences = NULL;
    }
}

template&lt;class T&gt;
inline void ReferenceCounter&lt;T&gt;::addRef()
{
    debug_assert((*pCountReferences) &gt;= 0, "Incorrect value of count references");
    ++(*pCountReferences);
}

template&lt;class T&gt;
inline void ReferenceCounter&lt;T&gt;::release()
{
    debug_assert((*pCountReferences) &gt; 0, "Incorrect value of count references");
    (*pCountReferences)--;

    if(pCountReferences != NULL &amp;&amp; *pCountReferences == 0)
    {
        destroyObject();
    }
}

template&lt;class T&gt;
inline uint32 ReferenceCounter&lt;T&gt;::getCountReferences() const
{
    return *pCountReferences;
}
</code></pre>

<p>This is my smart pointer :</p>

<pre><code>template&lt;class T&gt;
class SmartPtr
{
public:
    SmartPtr();
    SmartPtr(T* pInst);
    SmartPtr(const SmartPtr&lt;T&gt;&amp; rhs);
    ~SmartPtr();

    void operator = (const SmartPtr&lt;T&gt;&amp; rhs);
    T* operator -&gt; () const;
    T* getData() const;

    bool isNULL() const;

private:
    T* pInst;
};

template&lt;class T&gt;
SmartPtr&lt;T&gt;::SmartPtr() : pInst(NULL) {}

template&lt;class T&gt;
SmartPtr&lt;T&gt;::SmartPtr(T* pInst) : pInst(pInst) {}

template&lt;class T&gt;
SmartPtr&lt;T&gt;::~SmartPtr() 
{
    if(pInst != NULL)
    {
        pInst-&gt;release();
    }
}

template&lt;class T&gt;
SmartPtr&lt;T&gt;::SmartPtr(const SmartPtr&lt;T&gt;&amp; rhs)
{
    this-&gt;pInst = rhs.pInst;
    if(pInst != NULL)
    {
        pInst-&gt;addRef();
    }
}

template&lt;class T&gt;
void SmartPtr&lt;T&gt;::operator= (const SmartPtr&lt;T&gt;&amp; rhs)
{
    this-&gt;pInst = rhs.pInst;
    if(pInst != NULL)
    {
        pInst-&gt;addRef();
    }
}

template&lt;class T&gt;
T* SmartPtr&lt;T&gt;::operator-&gt;() const
{
    return pInst;
}

template&lt;class T&gt;
T* SmartPtr&lt;T&gt;::getData() const
{
    return pInst;
}

template&lt;class T&gt;
bool SmartPtr&lt;T&gt;::isNULL() const
{
    return pInst == NULL;
}
</code></pre>

<p>There are test of code  :</p>

<pre><code>#include &lt;iostream&gt;
#include "ReferenceCounter.h"
#include "SmartPtr.h"

using namespace std;

class B;

class A : public ReferenceCounter&lt;A&gt;
{
public:
    A();
    A(const A&amp; rhs);
    ~A();

    SmartPtr&lt;B&gt; getB();
    void operator = (const A&amp; rhs);

    private:
    void destroyObject();
    void shallowCopy(const A&amp; rhs);
 };

class B : public ReferenceCounter&lt;B&gt;
{
private:
    void destroyObject() {} ;
    void shallowCopy(const B&amp; rhs) {};
};

A::A()
{
    cout &lt;&lt; "Create object" &lt;&lt; endl;
}

A::A(const A&amp; rhs)
{
    shallowCopy(rhs);
    addRef();
    cout &lt;&lt; "copy constructor " &lt;&lt; endl;
}

A::~A()
{
    release();
}

void A::destroyObject()
{
    cout &lt;&lt; "destroy" &lt;&lt; endl;
}

void A::shallowCopy(const A&amp; rhs)
{
    this-&gt;pCountReferences = rhs.pCountReferences;
}

void A::operator = (const A&amp; rhs)
{
    shallowCopy(rhs);
    addRef();
    cout &lt;&lt; "operator = " &lt;&lt; endl;
}

SmartPtr&lt;B&gt; A::getB()
{
    return SmartPtr&lt;B&gt;(new B());
}

SmartPtr&lt;A&gt; getA()
{
    SmartPtr&lt;A&gt; a(new A());
    return a;
}

int main()
{
    getA();
    return 0;
}
</code></pre>

<p>This code is worked but below not called copy constructor of smart pointer when i debug this code . What problems happens below ??</p>

<pre><code>int main()
{
   A a;
   a.getB();
}
</code></pre>
