auto_ptr:
来源:互联网 发布:淘宝店铺如何认证 编辑:程序博客网 时间:2024/04/30 15:30
附录:auto_ptr的实现代码,来自Nicolai M. Josuttis
/* class auto_ptr
* - improved standard conforming implementation
*/
namespace std {
// auxiliary type to enable copies and assignments (now global)
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; // refers to the actual owned object (if any)
public:
typedef T element_type;
// constructor
explicit auto_ptr (T* ptr = 0) throw()
: ap(ptr) {
}
// copy constructors (with implicit conversion)
// - note: nonconstant parameter
auto_ptr (auto_ptr& rhs) throw()
: ap(rhs.release()) {
}
template<class Y>
auto_ptr (auto_ptr<Y>& rhs) throw()
: ap(rhs.release()) {
}
// assignments (with implicit conversion)
// - note: nonconstant parameter
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;
}
// destructor
~auto_ptr() throw() {
delete ap;
}
// value access
T* get() const throw() {
return ap;
}
T& operator*() const throw() {
return *ap;
}
T* operator->() const throw() {
return ap;
}
// release ownership
T* release() throw() {
T* tmp(ap);
ap = 0;
return tmp;
}
// reset value
void reset (T* ptr=0) throw() {
if (ap != ptr) {
delete ap;
ap = ptr;
}
}
/* special conversions with auxiliary type to enable copies and assignments
*/
auto_ptr(auto_ptr_ref<T> rhs) throw()
: ap(rhs.yp) {
}
auto_ptr& operator= (auto_ptr_ref<T> rhs) throw() { // new
reset(rhs.yp);
return *this;
}
template<class Y>
operator auto_ptr_ref<Y>() throw() {
return auto_ptr_ref<Y>(release());
}
template<class Y>
operator auto_ptr<Y>() throw() {
return auto_ptr<Y>(release());
}
};
}
- auto_ptr:
- Auto_ptr
- auto_ptr
- auto_ptr
- auto_ptr
- auto_ptr
- auto_ptr
- auto_ptr
- auto_ptr
- auto_ptr
- auto_ptr
- Auto_Ptr
- auto_ptr
- auto_ptr
- auto_ptr
- auto_ptr
- auto_ptr
- auto_ptr
- 关于"Client does not support authentication protocol requested by server”错误
- JConfig帮助文档翻译-初始化JConfig
- 逻辑题目
- FreeBSD服务器的安装与优化
- nBSD]我的OpenBSD 3.6基本应用 [2004-09-16]
- auto_ptr:
- (转载/摘操/保存)win2003下APACHE2.050+PHP5+MYSQL4.0.20+PHPMYADMIN2.57 的简易安装配置
- (转载)配置最新的PHP加MYSQL服务器
- Observer 模式
- 浅析本机API
- J2SE 5.0 Tomcat 5.5 尝鲜
- PASCAL 高级编程
- 35岁前应该做好的十件事
- 社会不会等待你成长