码迷,mamicode.com
首页 > 其他好文 > 详细

智能指针

时间:2016-04-27 19:02:46      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:c++   智能指针

智能指针就是智能/自动化的管理指针所指向的动态资源的释放

#include<iostream>

#include<cstdlib>

using namespace std;

//****************资源转移******************

//最好不要使用,会因为释放野指针造成程序奔溃

template<typename T>

class AutoPtr

{

public:

                AutoPtr( T* ptr )  

                                :_ptr( ptr)

                { }

                AutoPtr( AutoPtr<T >& ap)

                                :_ptr( ap._ptr)

                {

                                 ap._ptr = NULL ;

                }

                 AutoPtr<T >& operator=(const AutoPtr< T>& ap )

                {

                                 if (this != &ap)

                                {

                                                 if (_ptr != NULL )

                                                {

                                                                 delete _ptr;

                                                }

                                                _ptr = ap._ptr;

                                                 ap._ptr = NULL ;

                                }

                                 return *this ;

                }

                ~AutoPtr()

                {

                                 if (NULL != _ptr)

                                {

                                                 delete _ptr;

                                }

                }

private:

                 T* _ptr;

};


void FunTest()

{

                 int *p = new int[10];

                 delete[] p;

                 AutoPtr<int > ap1 = new int(10);

                 AutoPtr<int > ap2(ap1);    //把ap1这块空间交给ap2管理,不能通过ap1访问这块空间

                                          //函数结束的时候,释放ap1的时候,发现ap1成为野指针,程序崩溃

}


int main()

{


                system( "pause");

                 return 0;

}



//防拷贝

//将拷贝函数和赋值函数在类中声明为私有的,即使别人在外面可以定义,也不能调用

//若是声明为共有的,别人可以在类外定义并调用

template<typename T>

class ScoptArr

{

public:

                ScoptArr( T* ptr = NULL, size_t size = 0)

                                :_ptr( ptr)

                                , _size( size)

                { }

                 T& operator[] (size_t idx)

                {

                                 if (ide >= _size)

                                {

                                                 overflow_error err("数组访问越界" );

                                                                 throw err;

                                }

                                 return _ptr[idx ];

                }

                 const T & operator [](size_t idx) const;

                ~ScoptArr()

                {

                                 if (_ptr != NULL )

                                {

                                                 delete[] _ptr;

                                }

                }

private:

                ScoptArr(ScopedPtr< T> & ap);

                 ScoptArr<T > & operator =(const ScoptrArr &ap);

                 T* _ptr;

                 size_t  _size;

};





template<typename T>

class SharedPtr

{

public:

                SharedPtr( T* ptr = NULL)

                                :_ptr( ptr)

                                , _pCount( new int (0))

                {

                                 if (NULL != _ptr)

                                {

                                                *_pCount = 1;

                                }

                }

                SharedPtr( SharedPtr<T > & sp)

                                :_ptr( sp._ptr)

                                , _pCount( sp._pCount)

                {

                                (*_pCount)++;

                }

                 SharedPtr<T >& operator=(const SharedPtr< T>& sp )

                {

                                 if (this != &sp)

                                {

                                                 if (--(*_pCount) == 0&&(NULL !=_ptr)

                                                {

                                                                 delete _pCount;

                                                                 delete[] _ptr;

                                                }

                                                eles if (NULL == _ptr)

                                                {

                                                                 delete _pCount;

                                                }

                                                 else

                                                {

                                                                _ptr = sp._ptr;

                                                                _pCount = sp._pCount;

                                                                ++(*_pCount);

                                                }

                                }

                                 return *this ;

                }

                ~SharedPtr()

                {

                                 if (--(*_pCount) == 0)

                                {

                                                 delete[] _ptr;

                                                delelte _pCount;

                                }

                }

private:

                 T* _ptr;

                 int* _pCount;

};

情况一:

技术分享

情况二:

技术分享




智能指针

标签:c++   智能指针

原文地址:http://10921020.blog.51cto.com/10911020/1768279

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!