STL之智能指针

#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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值