shared_ptr智能指针的简单实现

shared_ptr

code1

#include 
using namespace std;

template <typename T>
class SmartPointer
{
public:
    //构造函数
    SmartPointer(T *p = 0) : _ptr(p), _reference_count(new size_t)
    {
        if (p)
            *_reference_count = 1;
        else
            *_reference_count = 0;
    }
    //拷贝构造函数
    SmartPointer(const SmartPointer &src)
    {
        if (this != &src)
        {
            _ptr = src._ptr;
            _reference_count = src._reference_count;
            (*_reference_count)++;
            cout << "Copy. count is " << *_reference_count << endl;
        }
    }
    //重载赋值操作符
    SmartPointer &operator=(const SmartPointer &src)
    {
        if (_ptr == src._ptr)
        {
            cout << "== count is " << *_reference_count << endl;
            return *this;
        }
        else
        {
            //存在不相等
            releaseCount();
            _ptr = src._ptr;
            _reference_count = src._reference_count;//重新赋值
            (*_reference_count)++;
            cout << "= count is " << *_reference_count << endl;
            return *this;
        }
    }

    //重载操作符
    T &operator*()
    {
        if (_ptr)
        {
            return *_ptr;
        }
        //throw exception
    }
    //重载操作符
    T *operator->()
    {
        if (_ptr)
        {
            return _ptr;
        }
        //throw exception
    }
    //析构函数
    ~SmartPointer()
    {
        cout << "Del start " << *_reference_count << endl;
        if (--(*_reference_count) == 0)
        {
            cout << *_reference_count << " " << *_ptr << endl;
            delete _ptr;
            delete _reference_count;
            cout << "Del." << endl;
        }
    }

private:
    T *_ptr;
    size_t *_reference_count;

    void releaseCount()
    {
        if (_ptr)
        {
            (*_reference_count)--;
            if ((*_reference_count) == 0)
            {
                delete _ptr;
                delete _reference_count;
                cout << "Del copy." << endl;
            }
            cout << "The copy ptr is exist. Count is " << *_reference_count << endl;
        }
    }
};

int main()
{
    SmartPointer<char> cp1(new char('a'));
    SmartPointer<char> cp2(cp1); // Copy. count is 2
    SmartPointer<char> cp3;
    cout<< "-----------cp3 = cp2----"<<endl;
    cp3 = cp2; //=  count is 3
    cout<< "-----------cp3 = cp1----"<<endl;
    cp3 = cp1; //The copy ptr is exist. Count is 2
    cout<< "-----------cp3 = cp3----"<<endl;
    cp3 = cp3; //del
    SmartPointer<char> cp4(new char('b'));
    cout<< "-----------cp3 = cp4----"<<endl;
    cp3 = cp4;

    cout<< "-----------DEL----"<<endl;
    return 0;
}

code2

#include 
using namespace std;
template <class T, class Del>
class SharePtr
{
public:
    SharePtr(T *ptr) //构造函数
        : _ptr(ptr), _ref(new int(1))
    {
        cout << "SharePtr(T* ptr)" << endl;
    }
    SharePtr(SharePtr &sp) //拷贝构造函数
    {
        cout << "SharePtr( SharePtr& sp)" << endl;
        _ptr = sp._ptr;
        _ref = sp._ref;
        (*_ref)++;
    }
    ~SharePtr() //析构函数
    {
        Release();
    }
    void Release()
    {
        if (--(*_ref) == 0)
        {
            delete _ref;
            _del(_ptr);
        }
    }
    T *operator->() //箭头操作符的重载
    {
        return _ptr;
    }
    T &operator*() //解引用操作符重载
    {
        return *_ptr;
    }
    SharePtr &operator=(SharePtr &sp) //赋值操作符的重载
    {
        if (_ptr != sp._ptr)
        {
            Release();
            _ptr = sp._ptr;
            _ref = sp._ref;
            (*_ref)++;
        }
        return *this;
    }
private:
    T *_ptr;
    int *_ref;
    Del _del; //-----这是一个对象函数决定着析构时以什么方式释放申请的空间
};

[1] https://blog.csdn.net/worldwindjp/article/details/18843087?depth_1-utm_source=distribute.pc_relevant_right.none-task&utm_source=distribute.pc_relevant_right.none-task

[2] https://www.cnblogs.com/wxquare/p/4759020.html

你可能感兴趣的:(C/C++)