auto_ptr :存在的缺点
1.只能一个对象拥有,不能一物二主
2.缺少对应用数和数组的支持
3.不能作为STL的容器元素
4.复制会传递所有权
Boost中的5类智能指针:
1.scoped_ptr:
2.scoped_array;
3.shared_ptr
4.shared_array
5.weak_ptr
前4中的智能指针是针对auto_ptr的指针缺点设计的
详细的内容:
1.autoptr的实现:《MoreEffectiveC++》中的一个实现:
template <class T>
class my_auto_ptr
{
public:
explicit my_auto_ptr(T *p = 0);
template <class U>
my_auto_ptr(my_auto_ptr & rhs);
~my_auto_ptr();
template<class U>
my_auto_ptr<T> & operator=(my_auto_ptr<U> & rhs);
T& operator*() const;
T* operator->() const;
T* get() const;
T* realse();
void reset(T *p = 0);
private:
T *_pointee;
template<class U>
friend class my_auto_ptr<U>;
};
#include "auto_ptr.h"
//构造函数
template<class T> inline my_auto_ptr<T>::my_auto_ptr(T * p) : _pointee(p)
{
}
//拷贝构造
template<class T> template <class U>inline my_auto_ptr<T>::my_auto_ptr(my_auto_ptr<T> &rhs):_pointee(rhs.realse())
{
}
//析构函数
template<class T> inline my_auto_ptr<T>::~my_auto_ptr()
{
delete _pointee;
}
//赋值操作
template<class T> template<class U> inline my_auto_ptr<T> & my_auto_ptr<T>::operator =(my_auto_ptr<U> &rhs)
{
if(this != &rhs)
{
reset(rhs.release());
}
return *this;
}
template<class T> inline T & my_auto_ptr<T>::operator *() const
{
return *_pointee;
}
template<class T> inline T* my_auto_ptr<T>::operator ->() const
{
return _pointee;
}
template<class T> inline T* my_auto_ptr<T>::get() const
{
return _pointee;
}
//释放所有权
template<class T> inline T* my_auto_ptr<T>::realse()
{
T *old_pointee = _pointee;
_pointee = 0;
return old_pointee;
}
template<class T> inline void my_auto_ptr<T>::reset(T *p = 0)
{
if(_pointee != p)
{
delete _pointee;
}
_pointee = p;
}