smart pointer reference counting in C++
<p>I am reading safe C++ and I  have question on reference counting. Question is provided in comment section in code below?</p>

<pre><code>#include &lt;iostream&gt;

using namespace std;

template &lt;typename T&gt;class RefCountPtr {
public: 
    explicit RefCountPtr(T* p = NULL) {
            Create(p);      
    }

    RefCountPtr(const RefCountPtr&lt;T&gt;&amp; rhs) {
        Copy(rhs);      
    }

    RefCountPtr&lt;T&gt;&amp; operator=(const RefCountPtr&lt;T&gt;&amp; rhs) {  
        if(ptr_ != rhs.ptr_) {
            Kill();         
            Copy(rhs);  
        }
        return *this;   
    }

    RefCountPtr&lt;T&gt;&amp; operator=(T* p) {
        if(ptr_ != p) { 
            Kill();     
            Create(p);  
        }   
        return *this;
    }   
    ~RefCountPtr() {
        Kill();
    }
    T* Get() const {
        return ptr_; 
    }

    T* operator-&gt;() const { 
        // SCPP_TEST_ASSERT(ptr_ != NULL, "Attempt to use operator -&gt; on NULL pointer.");   
        return ptr_;    
    }   

    T&amp; operator* () const { 
        // SCPP_TEST_ASSERT(ptr_ != NULL, "Attempt to use operator * on NULL pointer.");
        return *ptr_;   
    }

    int GetCount() { return *count_; }
private:    
    T*  ptr_;
    int*    count_;

    void Create(T* p) { 
        ptr_ = p;   
        if(ptr_ != NULL) {
            count_ = new int;
            *count_ = 1;    
        } else {
            count_ = NULL;  
        }   
    }   

    void Copy(const RefCountPtr&lt;T&gt;&amp; rhs) {  
        ptr_ = rhs.ptr_;
        count_ = rhs.count_;
        if(count_ != NULL)  
            ++(*count_);
    }   

    void Kill() {   
        if(count_ != NULL) {
            if(--(*count_) == 0) {  
                delete ptr_;    
                delete count_;  
            }   
        }   
    }
};

void main()
{

    int* pFirstInt = new int;

    RefCountPtr&lt;int&gt; refPointer(pFirstInt);

    cout &lt;&lt; "count: " &lt;&lt; refPointer.GetCount() &lt;&lt; std::endl;

    RefCountPtr&lt;int&gt; refSecondPointer = refPointer; // this calls copy constructor.

    cout &lt;&lt; "second count: " &lt;&lt; refSecondPointer.GetCount() &lt;&lt; std::endl;

    RefCountPtr&lt;int&gt; refThirdPointer;
    refThirdPointer = pFirstInt; **// Question why are we not incrementing same int pointer? How can we correct this?
                                 // If we are not incrementing same pointer why author has provided 
                                 // operator = for data type T? 
                                 // Note: As expected program is crashing while exiting as we are deleting int pointer twice.**

    std::cout &lt;&lt; "Third pointer: " &lt;&lt; refThirdPointer.GetCount() &lt;&lt; std::endl;

    RefCountPtr&lt;int&gt; refFourthPointer;
    refFourthPointer = refSecondPointer;

    cout &lt;&lt; "Fourth count: " &lt;&lt; refFourthPointer.GetCount() &lt;&lt; std::endl;

    return;
}

/*
count: 1
second count: 2
Third pointer: 1
Fourth count: 3
Press any key to continue . . .

*/
</code></pre>
