C++引用计数 && 智能指针

Posted on 2018-06-12 11:26  沙_shine  阅读(935)  评论(0编辑  收藏  举报

1. 引用计数

https://www.cnblogs.com/QG-whz/p/4777312.html

2. C++赋值运算符为什么要返回引用?

https://blog.csdn.net/pinganaini/article/details/50807513

https://blog.csdn.net/realdinghao/article/details/19899383

3.C++本质:“=”运算符的重载,深拷贝 浅拷贝

http://www.cnblogs.com/alexusli/archive/2008/08/27/1277683.html

https://www.cnblogs.com/Winston/archive/2008/06/03/1212700.html

4. C++拷贝构造函数和赋值运算符

https://www.cnblogs.com/wangguchangqing/p/6141743.html

5. 智能指针的实现

https://blog.csdn.net/qq_29003347/article/details/78495683

https://blog.csdn.net/worldwindjp/article/details/18843087

https://www.cnblogs.com/wxquare/p/4759020.html

https://blog.csdn.net/to_be_better/article/details/53570910

简单剖析:

https://www.cnblogs.com/lanxuezaipiao/p/4132096.html

https://www.jianshu.com/p/0300b2d833af

比较:

https://blog.csdn.net/skyroben/article/details/70195575

6. 实现代码(me)

#define M_share_ptr
#ifdef M_share_ptr

#include<memory>
#include<iostream>
#include<assert.h>
using namespace std;

template<typename T>
class SmartPointer
{
public:
    //构造函数
    SmartPointer(T* ptr = nullptr)
        :m_ptr(ptr)
    {
        if (ptr)
        {
            m_count = new size_t(1);
        }
        else
        {
            m_count = new size_t(0);
        }
    }
    //拷贝构造函数
    //拷贝构造函数必须以引用的方式传递参数。
    //这是因为,在值传递的方式传递给一个函数的时候,
    //会调用拷贝构造函数生成函数的实参。如果拷贝构造函数的参数
    //仍然是以值的方式,就会无限循环的调用下去,直到函数的栈溢出。
    SmartPointer(const SmartPointer& src)
    {
        if (this != &src)
        {
            this->m_ptr = src.m_ptr;
            this->m_count = src.m_count;
            (*this->m_count)++;//"->"的优先级高于"*"
        }
    }
    //拷贝赋值函数,重载operator=
    //会不会有连续赋值?只有返回一个引用,才可以循序连续赋值
    //返回值也可以支持连续赋值,会增加拷贝代价
    SmartPointer& operator=(const SmartPointer& src)
    {
        if (m_ptr == src.m_ptr)
            return *this;
        ReleaseCount();//如果为0,内存已经释放,后面的赋值怎么解释呢?
        m_ptr = src.m_ptr;//好像有点明白了.
        m_count = src.m_count;
        (*m_count)++;
        return *this;
    }
    //重载操作符
    T& operator*()
    {
        if (m_ptr)
            return *m_ptr;
        //throw exception
    }
    T* operator->()
    {
        if (m_ptr)
            return m_ptr;
        //throw exception
    }
    //析构函数
    ~SmartPointer()
    {
        (*m_count)--;
        if ((*m_count) == 0)
        {
            delete m_ptr;
            delete m_count;
        }
        else
        {
            cout << "还有" << *m_count << "个指针指向基础对象" << endl;
        }
    }
    size_t UseCount() const
    {
        return *m_count;
    }
private:
    T* m_ptr;
    size_t* m_count;//引用计数
    void ReleaseCount()
    {
        (*m_count)--;
        if ((*m_count) == 0)
        {
            delete m_ptr;
            delete m_count;
        }
    }
};
int main()
{
    SmartPointer<int> sm(new int(10));
    int test = 1;
    if (1 == test)
    {
        cout << "operator*():" << *sm << endl;
        cout << "reference counting:(sm)" << sm.UseCount() << endl;
        SmartPointer<int> sm2(sm);
        cout << "copy ctor reference counting:(sm)" << sm.UseCount() << endl;
        cout << "copy ctor reference counting:(sm2)" << sm2.UseCount() << endl;
        SmartPointer<int> sm3;
        sm3 = sm;
        cout << "copy operator= reference counting:(sm)" << sm.UseCount() << endl;
        cout << "copy ctor reference counting:(sm3)" << sm3.UseCount() << endl;
        cout << &sm << endl;
        cout << &sm2 << endl;
        cout << &sm3 << endl;
    }
    cout << "reference counting:(sm)" << sm.UseCount() << endl;
    //delete sm;

    getchar();
    return 0;
}


#endif/*M_share_ptr*/

 

运行结果: