boost库:Smart_ptr浅谈

无论C还是C++中,我们从一开始接触他们,老时就开始强调在使用后要对内存进行释放,以免发生内存泄露,但是,我们总有粗心的时候,C++中提出了只能指针的概念,用只能指针类来管理所申请的内存,伴随着函数的结束,智能指针自动调用析构函数对其进行释放。

智能指针(auto_ptr)
*auto_ptr指针的所有权可以转移,可以在函数只见传递,但同一时刻只能有一个auto_*ptr管理指针,当auto_ptr对象获得该空间的所有权后,用它来拷贝构造新的对象时,它所管理的指针的所有权也会发生改变,然后将它内部的指针赋空:
boost库:Smart_ptr浅谈_第1张图片
简单代码实现:

template<class _Ty>
class auto_ptr
{
public:
    auto_ptr(_Ty *_P=0):_Ptr(_P)
    {}
    auto_ptr(const auto_ptr<_Ty> &_Y):_Ptr(_Y.release())
    {}
    auto_ptr& operator=(auto_ptr& _a)
    {
        if(this != &_a)
        {
            reset(_a.release());
        }
        return *this;
    }
    ~auto_ptr()
    {delete _Ptr;}
public:
    void reset(_Ty* _p = 0)
    {
        if (_p != _Ptr)
        {
            delete _Ptr;
            _Ptr = _p;
        }
    }

    _Ty& operator*()const
    {
        return (*get());
    }
    _Ty* operator->()const
    {
        return (get());
    }
    _Ty* get()const
    {return _Ptr;}
    _Ty* release()const
    {
        _Ty* tmp = _Ptr;
        ((auto_ptr<_Ty>*)this)->_Ptr = 0;
        return tmp;
    }

private:
    _Ty *_Ptr;
};

int main()
{
    int *p = new int(10);
    auto_ptr<int> pa1(p);
    auto_ptr<int> pa2 = (pa1);
    auto_ptr<int> pa3;
    pa3 = pa2;
    //cout<<*pa2<<endl;
    //cout<<*pa1<<endl;
    return 0;
}

Scoped_ptr
scoped_ptr与auto_ptr十分相似,它时包装了new操作赋在对上分配的动态对象,能够保证动态创建的对象在任意时候都能被释放,相对于auto_ptr而言,scoped_ptr相对比较“自私”,它的所有权更见严格,一旦将所有权交给它,就无法再将所有权收回来,所以在scoped_ptr不允许进行赋值操作和拷贝构造。你可能会纠结它是如何实现的,就算你不给他赋值和拷贝构造函数,系统也会默认给出。其实很简单,将这两个函数声明为私有的就OK啦,下面我们一起来看下代码吧:

#include <iostream>
using namespace std;

template<typename _Ty>
class scoped_ptr
{
    scoped_ptr(const scoped_ptr &_s)
    {}
    scoped_ptr& operator=(scoped_ptr &_s)
    {}
public:
    explicit scoped_ptr(_Ty *_p = 0):_Ptr(_p)
    {}
    ~scoped_ptr()
    {}
public:
    void reset(_Ty *_p = 0)
    {
        if(_p != _Ptr){
            delete _Ptr;
            _Ptr = _p;
        }
    }
    _Ty& operator*()const
    {
        return *get();
    }
    _Ty* operator->()const
    {
        return (get());
    }
    _Ty* get()const
    {
        return _Ptr;
    }
    void swap(scoped_ptr &_s)
    {
        _Ty *tmp = _s._Ptr;
        _s._Ptr = _Ptr;
        _Ptr = tmp;
    }
private:
    _Ty *_Ptr;
};

int mian()
{
    int *p = new int(10);
    scoped_ptr<int> ps(p);
    cout << *ps << endl;

    return 0;
}

在类中成员默认为私有成员,所以这样就无法调动赋值操作和拷贝构造函数了。

scoped_array:
scoped_array主要是用来管理指针的,它的借口功能与scoped_ptr疾苦相同,scoped_array主要特点如下:
1、scoped_array在构造是接受的必须是new[]的结果;
2、scoped_array中没有“*” “->”操作赋的重载;
3、在析构时,用delete[]来释放内存;
4、重载[]运算符,使其能和普通数组一样用下标访问元素;
5、没有begin()、end()等类似容器的迭代器操作函数。

shared_ptr
shared_ptr与scoped_ptr相似,也是包装了new操作赋在堆上分配的动态对象,但它可以实现多个shared_ptr管理同一个指针,因为,它在实现时,在内便加入了使用计数器,只要拷贝一次引用计数器值加1,当引用计数器值不为一时部进行析构,这样防止了一块内存多次析构而导致的程序崩溃:
boost库:Smart_ptr浅谈_第2张图片

shared_ptr.h:

#ifndef _SHARED_PTR_H
#define _SHARED_PTR_H
//#include <iostream>
#include "shared_count.h"
#include "sp_counted_base.h"
//using namespace std;

template<typename _Ty>
class shared_ptr
{
public:
    shared_ptr():px(0)
    {
        cout << "Creat shared_ptr!!" << endl;
    }
    shared_ptr(const shared_ptr<_Ty> &r):px(r.px),pn(r.pn)
    {
        cout << "Creat shared_ptr!!" << endl;
    }
    shared_ptr(_Ty *p):px(p),pn(p)
    {
        cout << "Creat shared_ptr!!" << endl;
    }
    #if 1
    ~shared_ptr()
    {
        cout << " Free shared_ptr!!" << endl;
        if(use_count() == 0){
            cout << "Really free shared_ptr!!" << endl;
            cout << "haha use_count_ = "<<use_count()<<endl;
            px = 0;
        }
    }
    #endif
public:
    _Ty& operator*()const
    {
        return *px;
    }
    _Ty* operator->()const
    {
        return px;
    }
public:
    long use_count()const
    {
        return pn.use_count();
    }
public:
    void swap(shared_ptr<_Ty> &b)
    {
        _Ty *temp;
        temp = px;
        px = b.px;
        b.px = temp;
    }
private:
    shared_count<_Ty> pn;
    _Ty *px;
};

#endif

shared_count.h:

#ifndef _SHARED_COUNT_H
#define _SHARED_COUNT_H
#include <iostream>
#include "sp_counted_impl_xxx.h"
#include "sp_counted_base.h"
using namespace std;

template<typename _Ty>
class shared_count
{
public:
    shared_count():pi_(0)
    {
        cout << "Creat shared_counted!!" << endl;
    }
    shared_count(_Ty *p):pi_(new sp_counted_impl_xxx<_Ty>(p))
    {
        cout << "Creat shared_counted!!" << endl;
    }
    shared_count(const shared_count &r):pi_(r.pi_)
    {
        pi_->add_ref_copy();
    }
    shared_count& operator=(const shared_count &r)
    {
        pi_ = r.pi_;
        pi_->add_ref_copy();
    }
    ~shared_count()
    {
        cout << "Free shared_counted!!" << endl;
        pi_->~sp_counted_base();
        if(use_count() == 0){
            pi = 0;
        }
    }
public:
    long use_count()const
    {
        return pi_->use_count();
    }
    void decrement()
    {
        pi_->release();
        if(use_count == 0){
            delete this;
        }
    }
private:
    sp_counted_base *pi_;
};

#endif

sp_counted_base.h:

因为shared_count 里边的sp_counted_base *类型的pi_指向的是sp_counted_impl_xxx类里面的基类sp_counted_base,而基类无法调用子类的析构函数,而子类不析构基类也无法析构,所以,为了让他们能准确析构,在这里将sp_counted_base的析构函数定义为虚函数,在sp_counted_impl_xxx内重写。

#ifndef _SP_COUNTED_BASE_H
#define _SP_COUNTED_BASE_H
#include <iostream>
#include "sp_counted_impl_xxx.h"
using namespace std;

//template<typename _Ty>
class sp_counted_base
{
public:
    sp_counted_base():use_count_(1)
    {
        //use_count_ = 1;
        cout << "Creat sp_counted_base!!" << endl;
    }
    virtual ~sp_counted_base() //因为父类无法调用子类内部的函数,所以将其定义为虚函数,以保证它和它的子函数能够正常析构。
    {
         cout << "Free sp_counted_base" << endl;
    }
public:
    void add_ref_copy()
    {
        ++ use_count_;
    }
    long use_count()const
    {
        return use_count_;
    }
    void release()
    {
        -- use_count_;
    }
private:
    long use_count_;
};

#endif

sp_counted_impl_xxx.h:

#ifndef _SP_COUNTED_IMPL_XXX_H
#define _SP_COUNTED_IMPL_XXX_H
#include <iostream>
#include "sp_counted_base.h"
using namespace std;

template<typename _Ty>
class sp_counted_impl_xxx : public sp_counted_base
{
public:
    sp_counted_impl_xxx(_Ty *p):px_(p)
    {
        cout << "Creat sp_count_impl_xxx!!" << endl;
    }
    ~sp_counted_impl_xxx()
    {
        while(use_count()){
            this->release();
            cout << "\n\nuse_count() = " << use_count() << endl;
            if(use_count() == 0){
                delete px_;
                cout << "Free sp_count_impl_xxx!!" << endl;
            }
        }
    }
private:
    _Ty *px_;
};


#endif

main.cpp:

#include <iostream>
#include "shared_ptr.h"
#include "shared_count.h"
#include "sp_counted_base.h"
#include "sp_counted_impl_xxx.h"
using namespace std;

int main()
{
    int *p = new int(10);
    shared_ptr<int>pa(p);
    cout << "pa = " << *pa << endl;
    cout << "pa use_count = " << pa.use_count() << endl;

    shared_ptr<int> ps1 = pa;
    cout << "ps use_count = " << pa.use_count() << endl;
    shared_ptr<int> ps2;
    ps2 = ps1;
    cout << "ps use_count = " << pa.use_count() << endl;

    return 0;
}

你可能感兴趣的:(C++,智能指针,boost)