auto_ptr实现原型

来源:互联网 发布:企业数据备份解决方案 编辑:程序博客网 时间:2024/06/05 03:07
namespace std{
template<class Y>
struct auto_ptr_ref
{
Y* yp;
auto_ptr_ref(Y* rhs) :yp(rhs){


}
};


template<class T>
class auto_ptr{
private:
T* ap;
public:
typedef T element_type;
explicit auto_ptr(T* ptr = 0)throw() :ap(ptr){


}


auto_ptr(auto_ptr& rhs)throw():ap(rhs.release()){}
template<class Y>
auto_ptr(auto_ptr<Y>& rhs)throw():ap(rhs.rekease()){


}



auto_ptr& operator=(auto_ptr& rhs)throw(){
reset(rhs.release());
return *this; 
}


template<class Y>
auto_ptr& operator=(auto_ptr<Y>& rhs)throw(){
reset(rhs.release());
return *this;
}


~auto_ptr()throw(){
delete ap;
}


T* Get() const throw(){
return ap;


}


T& operator*()const throw(){
return *ap;
}


T* operator->()const throw(){
return ap;
}
\


T* release()throw(){
T* tmp(ap);
ap = 0;
return tmp;
}


};
}
0 0
原创粉丝点击