大家都用过smart pointer,但是如果要自己实现一个,有哪些函数需要实现呢,下面是auto_ptr的实现代码,我加了几行注释,帮助理解,同时列举哪些函数需要实现:
1. explicit版的构造函数;
2. 类型转换操作符,可以使auto_ptr<子类>到auto_ptr<父类>的赋值;
3. Copy构造函数;
4. pointer-to-member 操作符(* and –>);
5. 析构函数;
template<class _Ty>
class auto_ptr
{ // wrap an object pointer to ensure destruction
public:
typedef auto_ptr<_Ty> _Myt;
typedef _Ty element_type;
explicit auto_ptr(_Ty *_Ptr = 0) throw() // 【Must】使用explicit,避免该构造函数成为隐式类型转换构造函数
: _Myptr(_Ptr) // initialize the member in the initialization list, instead of calling (default constructor +assignment)
{ // construct from object pointer
}
auto_ptr(_Myt& _Right) throw() //【Must】Copy构造函数,auto_ptr的行为是将移除操作符右边的对象对资源的所有权
: _Myptr(_Right.release())
{ // construct by assuming pointer from _Right auto_ptr
}
auto_ptr(auto_ptr_ref<_Ty> _Right) throw()
{ // construct by assuming pointer from _Right auto_ptr_ref
_Ty *_Ptr = _Right._Ref;
_Right._Ref = 0; // release old
_Myptr = _Ptr; // reset this
}
template<class _Other>
operator auto_ptr<_Other>() throw() //【Must】隐式类型转换操作符,可以实现:auto_ptr<A> ptra(new A());auto_ptr<B> ptrb(new B()); ptra = ptrb; 这时要求B能隐式转换为A,比如B是A的子类;
{ // convert to compatible auto_ptr
return (auto_ptr<_Other>(*this));
}
template<class _Other>
operator auto_ptr_ref<_Other>() throw()
{ // convert to compatible auto_ptr_ref
_Other *_Cvtptr = _Myptr; // test implicit conversion
auto_ptr_ref<_Other> _Ans(_Cvtptr);
_Myptr = 0; // pass ownership to auto_ptr_ref
return (_Ans);
}
template<class _Other>
_Myt& operator=(auto_ptr<_Other>& _Right) throw()//【Must】赋值操作符,其行为跟Copy构造函数一致
{ // assign compatible _Right (assume pointer)
reset(_Right.release());
return (*this);
}
template<class _Other>
auto_ptr(auto_ptr<_Other>& _Right) throw() //【Must】Copy构造函数,可以实现auto_ptr<子类>到auto_ptr<父类>的转化
: _Myptr(_Right.release())
{ // construct by assuming pointer from _Right
}
_Myt& operator=(_Myt& _Right) throw() //【Must】赋值操作符
{ // assign compatible _Right (assume pointer)
reset(_Right.release());
return (*this);
}
_Myt& operator=(auto_ptr_ref<_Ty> _Right) throw()
{ // assign compatible _Right._Ref (assume pointer)
_Ty *_Ptr = _Right._Ref;
_Right._Ref = 0; // release old
reset(_Ptr); // set new
return (*this);
}
~auto_ptr() //【Must】析构函数
{ // destroy the object
delete _Myptr;
}
_Ty& operator*() const throw() // pointer-to-member 操作符
{ // return designated value
return (*get());
}
_Ty *operator->() const throw() // pointer-to-member 操作符
{ // return pointer to class object
return (get());
}
_Ty *get() const throw() //获取原始资源
{ // return wrapped pointer
return (_Myptr);
}
_Ty *release() throw() //
{ // return wrapped pointer and give up ownership
_Ty *_Tmp = _Myptr;
_Myptr = 0;
return (_Tmp);
}
void reset(_Ty *_Ptr = 0)
{ // destroy designated object and store new pointer
if (_Ptr != _Myptr)
delete _Myptr;
_Myptr = _Ptr;
}
private:
_Ty *_Myptr; // the wrapped object pointer
};
class auto_ptr
{ // wrap an object pointer to ensure destruction
public:
typedef auto_ptr<_Ty> _Myt;
typedef _Ty element_type;
explicit auto_ptr(_Ty *_Ptr = 0) throw() // 【Must】使用explicit,避免该构造函数成为隐式类型转换构造函数
: _Myptr(_Ptr) // initialize the member in the initialization list, instead of calling (default constructor +assignment)
{ // construct from object pointer
}
auto_ptr(_Myt& _Right) throw() //【Must】Copy构造函数,auto_ptr的行为是将移除操作符右边的对象对资源的所有权
: _Myptr(_Right.release())
{ // construct by assuming pointer from _Right auto_ptr
}
auto_ptr(auto_ptr_ref<_Ty> _Right) throw()
{ // construct by assuming pointer from _Right auto_ptr_ref
_Ty *_Ptr = _Right._Ref;
_Right._Ref = 0; // release old
_Myptr = _Ptr; // reset this
}
template<class _Other>
operator auto_ptr<_Other>() throw() //【Must】隐式类型转换操作符,可以实现:auto_ptr<A> ptra(new A());auto_ptr<B> ptrb(new B()); ptra = ptrb; 这时要求B能隐式转换为A,比如B是A的子类;
{ // convert to compatible auto_ptr
return (auto_ptr<_Other>(*this));
}
template<class _Other>
operator auto_ptr_ref<_Other>() throw()
{ // convert to compatible auto_ptr_ref
_Other *_Cvtptr = _Myptr; // test implicit conversion
auto_ptr_ref<_Other> _Ans(_Cvtptr);
_Myptr = 0; // pass ownership to auto_ptr_ref
return (_Ans);
}
template<class _Other>
_Myt& operator=(auto_ptr<_Other>& _Right) throw()//【Must】赋值操作符,其行为跟Copy构造函数一致
{ // assign compatible _Right (assume pointer)
reset(_Right.release());
return (*this);
}
template<class _Other>
auto_ptr(auto_ptr<_Other>& _Right) throw() //【Must】Copy构造函数,可以实现auto_ptr<子类>到auto_ptr<父类>的转化
: _Myptr(_Right.release())
{ // construct by assuming pointer from _Right
}
_Myt& operator=(_Myt& _Right) throw() //【Must】赋值操作符
{ // assign compatible _Right (assume pointer)
reset(_Right.release());
return (*this);
}
_Myt& operator=(auto_ptr_ref<_Ty> _Right) throw()
{ // assign compatible _Right._Ref (assume pointer)
_Ty *_Ptr = _Right._Ref;
_Right._Ref = 0; // release old
reset(_Ptr); // set new
return (*this);
}
~auto_ptr() //【Must】析构函数
{ // destroy the object
delete _Myptr;
}
_Ty& operator*() const throw() // pointer-to-member 操作符
{ // return designated value
return (*get());
}
_Ty *operator->() const throw() // pointer-to-member 操作符
{ // return pointer to class object
return (get());
}
_Ty *get() const throw() //获取原始资源
{ // return wrapped pointer
return (_Myptr);
}
_Ty *release() throw() //
{ // return wrapped pointer and give up ownership
_Ty *_Tmp = _Myptr;
_Myptr = 0;
return (_Tmp);
}
void reset(_Ty *_Ptr = 0)
{ // destroy designated object and store new pointer
if (_Ptr != _Myptr)
delete _Myptr;
_Myptr = _Ptr;
}
private:
_Ty *_Myptr; // the wrapped object pointer
};