C++智能指针

智能指针是用模板实现,创建对象后,对象管理了资源,然后通过析构函数释放掉该资源,以防内存泄漏。

auto_ptr(空间管理权限的转移,永远只有一个指针指向动态开辟的空间)
#include <iostream>
using namespace std;

template<class T>
class AutoPtr
{
public:
    AutoPtr(T* p = NULL)
        :_p(p)
    {
        cout<<"AutoPtr()"<<endl;
    }

    AutoPtr(AutoPtr<T>& ap)
        :_p(ap._p)
    {
        ap._p = NULL;
    }

    AutoPtr<T>& operator =(AutoPtr<T>& ap)
    {
        if(this != &ap)
        {
            if(_p != NULL)
                delete _p;
            _p = ap._p;
            ap._p = NULL;
        }
        return *this;
    }

    ~AutoPtr()
    {
        cout<<"~AutoPtr()"<<endl;
        if(_p)
            delete _p;
    }

    T& operator *()
    {
        return *_p;
    }

    T* operator ->()
    {
        return _p;
    }

private:
    T* _p;
};

struct A
{
    int a;
    int b;
};

int main()
{
    AutoPtr<int> ap1(new int(1));
    AutoPtr<int> ap2(ap1);
    AutoPtr<int> ap3(new int(2));
    ap3 = ap2;

    //cout<<*ap1<<endl;//非法访问
    //cout<<*ap2<<endl;//非法访问
    cout<<*ap3<<endl;


    AutoPtr<A> ap4(new A);
    ap4->a = 1;
    ap4->b = 2;
    cout<<ap4->a<<endl;
    cout<<ap4->b<<endl;

    return 0;
}
auto_ptr(用_owner来标记空间的释放权,防止重复释放同一块空间)
#include <iostream>
using namespace std;

template<class T>
class AutoPtr
{
public:
    AutoPtr(T* p = NULL)
        :_p(p)
        ,_owner(true)
    {
        if(_owner == NULL)
            _owner = false;
    }

    AutoPtr(AutoPtr<T>& ap)
        :_p(ap._p)
        ,_owner(true)
    {
        ap._owner = false;
    }

    AutoPtr<T>& operator =(AutoPtr<T>& ap)
    {
        if(this != &ap)
        {
            if(_p)
                delete _p;

            _p = ap._p;
            _owner = true;
            ap._owner = false;
        }
        return *this;
    }

    ~AutoPtr()
    {
        if(_owner)
        {
            delete _p;
            _p = NULL;
            _owner = false;
        }
    }

    T& operator *()
    {
        return *_p;
    }

    T* operator ->()
    {
        return _p;
    }

private:
    T* _p;
    mutable bool _owner;
};

struct A
{
    int a;
    int b;
};

int main()
{
    AutoPtr<int> ap1(new int(1));
    AutoPtr<int> ap2(ap1);
    AutoPtr<int> ap3(new int(2));
    ap3 = ap2;

    cout<<*ap1<<endl;//非法访问
    cout<<*ap2<<endl;//非法访问
    cout<<*ap3<<endl;


    AutoPtr<A> ap4(new A);
    ap4->a = 10;
    ap4->b = 20;
    cout<<ap4->a<<endl;
    cout<<ap4->b<<endl;

    return 0;
}
scoped_ptr(防止拷贝,防止赋值)
#include <iostream>
using namespace std;

template<class T>
class ScopedPtr
{
public:
    friend void Fun();

    ScopedPtr(T* p = NULL)
    {}

    T& operator *()
    {
        return *_p;
    }

    ~ScopedPtr()
    {
        if(_p)
        {
            delete _p;
            _p = NULL;
        }
    }
private:
    ScopedPtr(ScopedPtr<T>& sp);
    ScopedPtr<T>& operator =(const ScopedPtr<T>& sp);

    T* _p;
};

void Fun()
{
    ScopedPtr<int> sp1(new int);
    ScopedPtr<int> sp2;
    //ScopedPtr<int> sp3(sp1);//友元函数不是类的成员函数,同样无法访问私有成员
}

int main()
{
    Fun();

    ScopedPtr<int> sp1(new int(1));
    ScopedPtr<int> sp2;
    //ScopedPtr<int> sp3(sp1);//类外不能访问类的私有成员
    return 0;
}
scoped_array管理数组
#include <iostream>
using namespace std;

template<class T>
class ScopedArray
{
public:
    ScopedArray(T* p = NULL)
        :_p(p)
    {}

    ~ScopedArray()
    {
        if(_p)
        {
            delete _p;
            _p = NULL;
        }
    }

    T& operator [](int index)
    {
        return _p[index];
    }

    const T& operator [](int index)const
    {
        return _p[index];
    }
private:
    ScopedArray(ScopedArray<T>& sp);
    ScopedArray<T>& operator =(const ScopedArray<T>& sp);

    T* _p;
};

int main()
{
    ScopedArray<int> sp1(new int[10]);
    sp1[0] = 0;
    sp1[1] = 1;

    return 0;
}
shared_ptr(通过引用计数实现对空间的管理)
#include <iostream>
using namespace std;

template<class T>
class SharedPtr
{
public:
    SharedPtr(T* p =NULL)
        :_p(p)
        ,_pcount(NULL)
    {
        if(_p)
            _pcount = new int(1);
    }

    SharedPtr(const SharedPtr<T>& sp)
        :_p(sp._p)
        ,_pcount(sp._pcount)
    {
        (*sp._pcount)++;
    }

    SharedPtr<T>& operator =(const SharedPtr<T>& sp)
    {
        if(this != &sp)
        {
            Release();

            _p = sp._p;
            _pcount = sp._pcount;
            (*sp._pcount)++;
        }
        return *this;
    }

    ~SharedPtr()
    {
        Release();
    }

    T& operator *()
    {
        return *_p;
    }

    T* operator ->()
    {
        return _p;
    }
private:
    void Release()
    {
        if(_p && (--(*_pcount)) == 0)
        {
            delete _p;
            _p = NULL;

            delete _pcount;
            _pcount = NULL;
        }
    }

    T* _p;
    int* _pcount;
};

int main()
{
    SharedPtr<int> sp1(new int(1));
    SharedPtr<int> sp2(sp1);

    SharedPtr<int> sp3(new int(3));
    sp3 = sp1;

    return 0;
}
shared_array管理数组
#include <iostream>
using namespace std;

template<class T>
class SharedArray
{
public:
    SharedArray(T* p =NULL)
        :_p(p)
        ,_pcount(NULL)
    {
        if(_p)
            _pcount = new int(1);
    }

    SharedArray(const SharedArray<T>& sp)
        :_p(sp._p)
        ,_pcount(sp._pcount)
    {
        (*sp._pcount)++;
    }

    SharedArray<T>& operator =(const SharedArray<T>& sp)
    {
        if(this != &sp)
        {
            Release();

            _p = sp._p;
            _pcount = sp._pcount;
            (*sp._pcount)++;
        }
        return *this;
    }

    ~SharedArray()
    {
        Release();
    }

    T& operator *()
    {
        return *_p;
    }

    T* operator ->()
    {
        return _p;
    }

    T& operator [](size_t index)
    {
        return _p[index];
    }

    const T& operator [](size_t index)const
    {
        return _p[index];
    }
private:
    void Release()
    {
        if(_p && (--(*_pcount)) == 0)
        {
            delete _p;
            _p = NULL;

            delete _pcount;
            _pcount = NULL;
        }
    }

    T* _p;
    int* _pcount;
};

int main()
{
    SharedArray<int> sp(new int[10]);
    sp[0] = 0;
    sp[1] = 1;
    sp[2] = 2;

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值