HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Simple shared pointer

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
pointersimpleshared

Problem

I wrote a simple shared pointer, which I think works pretty well. I would like to see your review of it.

This is the header file:

#pragma once

#include 

template 
class SharedPointer
{
private:
    T* ptr;
    int* counter;

    void swapp(SharedPointer& first, SharedPointer& second);

public:
    SharedPointer(void);
    SharedPointer(T* val);
    SharedPointer(const SharedPointer& sp);

    T& operator*();
    T* operator->();
    SharedPointer& operator=(const SharedPointer& sp);
    ~SharedPointer(void);
};

template 
void SharedPointer::swapp(SharedPointer& first, SharedPointer& second)
{
    using std::swap;

    swap(first.ptr, second.ptr);
    swap(first.counter, second.counter);
}

template 
SharedPointer::SharedPointer(void)
{
    ptr = new T;
    counter = new int;
    *counter = 1;
}

template 
SharedPointer::SharedPointer(T* val) : ptr(val)
{
    counter = new int;
    *counter = 1;
}

template 
SharedPointer::SharedPointer(const SharedPointer& sp)
{
    ptr = sp.ptr;
    counter = sp.counter;
    ++(*counter);
}

template 
T& SharedPointer::operator*()
{
    return *ptr;
}

template 
T* SharedPointer::operator->()
{
    return ptr;
}

template 
SharedPointer& SharedPointer::operator=(const SharedPointer& sp)
{
    SharedPointer temp(sp);
    swapp(*this,temp);

    return *this;
}

template 
SharedPointer::~SharedPointer(void)
{
    if(--(*counter) == 0)
    {
        delete ptr;
        delete counter;
    }
}

Solution

Foreword

When developing your own version of some standard library or tool, I suggest you to specify how close to the standard version is along with the purpose behind it.

So, for istance:

  • Is it standard compliant?



  • Does it address anything missing?



Interface

What I find missing:

  • How can I reset the pointer?



  • How can I know how many "visitors" there are(that is, how do I get counter)? Note: It could be useful, but don't go too far and break the encapsulation!



  • An efficient way to visit the object, that is an alternative to std::weak_ptr:



  • How do I allocate/deallocate particular objects?



Implementation

  • #pragma once is not standard. Although almost all compilers implement it and seems like to be faster than header guards, I recommend these last ones at least until it gets standardized.



  • Why counter is allocated dynamically?



  • I understand you may not name swapp swap; however, there are better names, such as:



  • m_swap



  • internal_swap



  • C++, unlike C, makes no difference between function() and function(void).


Yes, they're both correct; but the first one is much more common.

  • new can throw std::bad_alloc. Especially in the second constructor, if new threw when allocating/constructing counter, ptr would be leaked.

Context

StackExchange Code Review Q#59004, answer score: 4

Revisions (0)

No revisions yet.