Boost库之shared_ptr应用

概解:
本文是自己在学习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;
       
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值