zoukankan      html  css  js  c++  java
  • 标准库shared_ptr智能指针的实现

    目前测试功能正常。若有不完善的地方在改进吧。时候不早了睡觉去,哎,翘课会被抓,不冒险了。晚安全世界O(∩_∩)O

      1 /************************************************************************* 
      2 *my shared_ptr:  share_ptr
      3 *author:ERIC
      4 *blog:http://www.ilovecpp.com
      5 *time:2015-5-28 01:36:43
      6 *************************************************************************/
      7 
      8 template <typename T> class share_ptr {
      9 private:
     10     T* __ptr;
     11     int* __pcounts;
     12 public:
     13     share_ptr(T* p= NULL);
     14     share_ptr(const share_ptr<T>& src);
     15     share_ptr& operator=(share_ptr<T>& src);
     16     ~share_ptr();
     17     operator bool() const;//支持if(p)形式
     18     T* operator-> () const;
     19     T& operator*() const;
     20     T* get() const;
     21     int use_counts() const;//返回引用计数
     22     bool unique() const;//当前智能指针是否唯一
     23     void swap(share_ptr& rhs);//交换,成员函数
     24     template <typename Type>//友元函数,交换两个智能指针
     25     friend void swap(share_ptr<Type>& lhs,share_ptr<Type>& rhs);
     26 };
     27 
     28 template<typename T>
     29 share_ptr<T>::share_ptr(T* p)
     30     :__ptr(p),__pcounts(new int(0))
     31 {
     32     if(__ptr)
     33         *__pcounts = 1;
     34 }
     35 
     36 template<typename T>
     37 share_ptr<T>::~share_ptr()
     38 {
     39     --*__pcounts;
     40     if(*__pcounts == 0)
     41     {//空智能指针这里delete  __ptr也安全,delete NULL;
     42         delete __pcounts;
     43         delete __ptr;
     44     }
     45 }
     46 
     47 /*__ptr(new T(src)) 很重要 ,如果直接__ptr(new T)
     48 *会由于T类没有默认构造函数而出错
     49 *测试的时候才发现这个问题的
     50 */
     51 template<typename T>
     52 share_ptr<T>::share_ptr (const share_ptr<T>& src)
     53     :__pcounts(new int),__ptr(new T(src))
     54 {
     55     ++*src.__pcounts;
     56     __ptr = src.__ptr;
     57     __pcounts = src.__pcounts;
     58 }
     59 
     60 template <typename T>
     61 share_ptr<T>& share_ptr<T>::operator= (share_ptr<T>& src)
     62 {
     63     --*__pcounts;
     64     //如果是空智能指针的话 __pcounts == -1,那块内存也得释放
     65     if(*__pcounts == 0 || *__pcounts == -1)
     66         delete __pcounts;
     67     ++*src.__pcounts;
     68     __ptr = src.__ptr;
     69     __pcounts = src.__pcounts;
     70     return *this;
     71 }
     72 
     73 //支持if(p)这样的操作
     74 template<typename T>
     75 share_ptr<T>::operator bool() const
     76 {
     77     return __ptr;
     78 }
     79 
     80 template<typename T>
     81 T* share_ptr<T>::operator->() const
     82 {
     83     return __ptr;
     84 }
     85 
     86 template<typename T>
     87 T& share_ptr<T>::operator*() const
     88 {
     89     return *__ptr;
     90 }
     91 
     92 
     93 template<typename T>
     94 T* share_ptr<T>::get() const
     95 {
     96     return __ptr;
     97 }
     98 
     99 template<typename T>
    100 int share_ptr<T>::use_counts() const
    101 {
    102     return *__pcounts;
    103 }
    104 
    105 template<typename T>
    106 bool share_ptr<T>::unique() const
    107 {
    108     if(*__pcounts == 1)
    109         return true;
    110     else
    111         return false;
    112 }
    113 
    114 template<typename T>
    115 void share_ptr<T>::swap(share_ptr<T>& rhs)
    116 {
    117     T* tmpPtr = rhs.__ptr;
    118     rhs.__ptr = __ptr;
    119     __ptr = tmpPtr;
    120     int* tmpPcounts = rhs.__pcounts;
    121     rhs.__pcounts = __pcounts;
    122     __pcounts = tmpPcounts; 
    123 }
    124 
    125 template<typename T>
    126 void swap(share_ptr<T>& lhs,share_ptr<T>& rhs)
    127 {
    128     T* tmpPtr = rhs.__ptr;
    129     rhs.__ptr = lhs.__ptr;
    130     lhs.__ptr = tmpPtr;
    131     int* tmpPcounts = rhs.__pcounts;
    132     rhs.__pcounts = lhs.__pcounts;
    133     lhs.__pcounts = tmpPcounts; 
    134 }
    135 
    136 //c++11 make_shared<T>(args) 
    137 template<typename T>
    138 share_ptr<T> make_share(T args)
    139 {
    140     return new T(args);
    141 }
  • 相关阅读:
    jQuery 源码解析(二十四) DOM操作模块 包裹元素 详解
    jQuery 源码解析(二十三) DOM操作模块 替换元素 详解
    jQuery 源码解析(二十二) DOM操作模块 复制元素 详解
    jQuery 源码分析(二十一) DOM操作模块 删除元素 详解
    jQuery 源码分析(二十) DOM操作模块 插入元素 详解
    jQuery 源码分析(十九) DOM遍历模块详解
    python 简单工厂模式
    python 爬虫-协程 采集博客园
    vue 自定义image组件
    微信小程序 image组件坑
  • 原文地址:https://www.cnblogs.com/ittinybird/p/4534842.html
Copyright © 2011-2022 走看看