STL之智能指针

来源:互联网 发布:nginx mysql 安装 编辑:程序博客网 时间:2024/05/21 06:55
#ifndef __MEMORY_H#define __MEMORY_H#include"Allocator.h"#include"construct.h"#include"uninitialized.h"template<class _Tp>class __auto_ptr{private:_Tp *_M_ptr;public:typedef _Tp element_type;explicit __auto_ptr(_Tp* __p = 0) :_M_ptr(__p){}__auto_ptr(__auto_ptr& __a) :_M_ptr(__a.release()){}template<class _Tp1>__auto_ptr(__auto_ptr<_Tp1>& __a) : _M_ptr(__a.release()){}__auto_ptr& operator=(__auto_ptr& __a){if (&__a != this){delete _M_ptr;_M_ptr = __a.release();}return *this;}template<class _Tp1>__auto_ptr& operator=(__auto_ptr<_Tp1>& __a){if (__a.get() != this.get()){delete _M_ptr;_M_ptr = __a.release();}return *this;}~__auto_ptr(){delete _M_ptr;}_Tp& operator*()const{ return *_M_ptr; }_Tp* operator->()const{ return _M_ptr; }_Tp* get()const{ return _M_ptr; }_Tp* release(){_Tp* __tmp = _M_ptr;_M_ptr = 0;return __tmp;}void reset(_Tp* __p = 0){delete _M_ptr;_M_ptr = __p;}private:template<class _Tp1>struct auto_ptr_ref{_Tp1* _M_ptr;auto_ptr_ref(_Tp1* __p) :_M_ptr(__p){}};public:__auto_ptr(auto_ptr_ref<_Tp> __ref) :_M_ptr(__ref._M_ptr){}template<class _Tp1>operator auto_ptr_ref<_Tp1>(){return auto_ptr_ref<_Tp>(this->release());}template<class _Tp1>operator __auto_ptr<_Tp1>(){return __auto_ptr<_Tp1>(this->release());}};#endif

0 0
原创粉丝点击