c++智能指针
来源:互联网 发布:郑州美工培训学校 编辑:程序博客网 时间:2024/06/04 18:36
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;
}
- c++:智能指针
- C++:智能指针
- 【C++】智能指针
- 【C++】智能指针
- C++PJ智能指针
- 【c++】智能指针
- 【C++】智能指针auto_ptr
- C++,智能指针
- C++,boost智能指针
- 智能指针模拟C
- [C++]智能指针
- 【C++】 浅析智能指针
- C++_智能指针
- C++:初识智能指针
- C++::智能指针
- 【C++】智能指针
- c智能指针:unique_ptr
- 【C++】智能指针
- Java多线程(全)学习笔记(中)
- Kosaraju算法的分析和证明
- x-loader 以及 如何向nand中写入x-loader
- UML总结
- 节点移动
- c++智能指针
- 插入算法 二分搜索 读数据结构与算法-C++
- 6.087 Practical Programming in C, lec13
- 获取节点
- 使用ingress qdisc和ifb进行qos
- Mac使用git/github小结
- javascript 中的function当做对象模型的时候怎样理解
- nerd实验室(一)
- 编译IMQ过程