Smart Pointers with Reference Counting - C++
Review

Issue

My first issues is that using your shared pointer is not safe. Once I give you the pointer. You are responsible for deleting it all situations (especially when exceptions happen).

But if the call to new PointerWrapper(p, 1) fails. ie it throws BadAlloc then you will leak my object. So you need to watch out for that allocation and delete the pointer immediately if you throw.

Missing Feature

You have not implemented move semantics. This is absolutely essential.

Your object is missing a swap function.

Design

I don't think the class PointerWrapper buys you ay functionality or makes the code any easier to write.

nullptr

Prefer to use nullptr over 0 as the non existing pointer. 0 is a number and can easily be converted to other things when you least expect it. nullptr of std::nullptr_t and can only be assigned to pointers and is not auto converted into other things.

General grumbles

I think the assignment operator is overly complex. It would be much simpler to write this as using the standard copy and swap idiom.

    SmartPtr&amp; operator=(const SmartPtr&amp; copyObj)
    {
         SmartPtr  copy(copyObj);
         copy.swap(*this);
         return *this;
    }


This logic seems a bit overly complex.

        unsigned int preReleaseRefCount = ptrWrapper_-&gt;getRefCount();
        ptrWrapper_-&gt;decrementRefCount();
        if(preReleaseRefCount==1)
        {
          delete ptrWrapper_;
          ptrWrapper_ = 0;
        }


Personally I would have got decrementRefCount() to return true if it deleted the object.

        if (ptrWrapper_-&gt;decrementRefCount())
        {
          delete ptrWrapper_;
        }


No point in setting the ptrWrapper_ to nullptr

           ptrWrapper_ = 0;


When the very next statement sets it to another value.

        ptrWrapper_ = copyObj-&gt;ptrWrapper_;


Self Plug

These articles are a good read (I wrote them):

Unique Ptr
Shared Ptr
Constructor