概解:
本文是自己在学习boost库完全开发的时候顺手做的笔记,摘出其中自认为是比较重要的部分以作整理并写成博文,分享出来,很是奇怪csdn代码块那块有颜色代码,不知道怎么去掉、、、因为没咋仔细研究、、、所以这编将就先弄着,等有时间再仔细研究一番这个:
shared_ptr非常有价值、非常重要、非常有用!
shared_ptr是一个最像指针的"智能指针",是boost::smart_ptr库中最有价值、最重要的组成部分,也是最有用的,Boost库中许多组件--甚至还包括其他一些领域的智能指针都使用了shared_ptr。
shared_ptr与scoped_ptr一样包装了new操作符在堆上分配的动态对象,但它实现的是引用计数型的智能指针,可以被自由地拷贝和赋值,在任意的地方共享它。当没有代码使用(引用计数为0)它才删除呗包装的动态分配的对象,shared_ptr也可以被安全的放到标准容器中,并弥补了auto_ptr因为转移语义而不能把指针作为STL容器元素的缺陷;
类摘要:
template<class T>
class shared_ptr
{
public:
typdef T elment_type;
shared_ptr();
template<class Y> explicit shared_ptr(Y *p);
template<class Y,class D> shared_ptr(Y *p,D d);
~shared_ptr();
shared_ptr(shared_ptr const &r);
template<class Y>explicit shared_ptr(std::auto_ptr<Y> &r);
shared_ptr& operator=(shared_ptr const &r);
template<class Y>shared_ptr &operator=(shared_ptr<Y> const &r);
template<class Y>shared_ptr &operator=(std::auto_ptr<Y> &r);
void reset();
template<class Y>void reset(Y *p);
template<class Y,class D>void reset(Y *p,D d);
T &operator*() const;