• 支持引用计数的智能指针类模板


    智能指针算是很多人喜欢思考的一种内存管理方案了...虽然这种方案本身存在一些硬伤,但是在很多需要智能,且使用方式相对较简单的场合里应用还是比较多的.

    先发一个我最初写好的版本:

    [cpp] view plaincopy
     
    1. //////////////////////////////////////////////////////////////////  
    2. // TSmartPtr - 智能指针类模板  
    3. //  
    4. // Author:  木头云  
    5. // Blog:    http://hi.baidu.com/markl22222  
    6. // E-Mail:  mark.lonr@tom.com  
    7. // Version: 1.0.819.1654(2009/08/19)  
    8. //  
    9. // History:  
    10. //  - 1.0.813.1148(2009/08/13)  @ 完成基本的类模板构建  
    11. //  - 1.0.814.1758(2009/08/14)  + 添加TSmartPtr类相关操作符重载(==)  
    12. //                              + 添加CSmartPtrManager智能指针管理类及全局的智能指针管理链表  
    13. //  - 1.0.817.1508(2009/08/17)  ^ 优化CSmartPtrManager使之支持引用计数  
    14. //                              + 添加TSmartPtr类相关操作符重载(*)  
    15. //  - 1.0.818.1600(2009/08/18)  ^ 优化CSmartPtrManager指针管理链表的list为map  
    16. //                              - 删除CSmartPtrManager类的Find()函数  
    17. //                              = 调整TSmartPtr类模板参数,将is_ary改为DelFunction函数指针  
    18. //                              = 调整CSmartPtrManager类,将其函数改为静态函数,并定义自动回收对象CAutoRecycle  
    19. //                              + 添加CDelFunc类,用于统一管理指针删除算法  
    20. //  - 1.0.819.1654(2009/08/19)  + TSmartPtr类添加相关操作符重载([],+,-)  
    21. //                              # 修正当指针自我复制时导致的指针被销毁的bug  
    22. //////////////////////////////////////////////////////////////////  
    23.   
    24. #pragma once  
    25.   
    26. #include <map>  
    27.   
    28. using namespace std;  
    29.   
    30. //////////////////////////////////////////////////////////////////  
    31.   
    32. // 指针类型重定义  
    33. typedef void* _Ptr;  
    34.   
    35. // 指针销毁函数指针重定义  
    36. typedef void (*_DelFun)(_Ptr p);  
    37.   
    38. //////////////////////////////////////////////////////////////////  
    39.   
    40. // CDelFunc - 指针删除算法类  
    41. class CDelFunc  
    42. {  
    43. public:  
    44.     inline static void DelSingle(_Ptr p)  
    45.     {  
    46.         if(p) delete p;  
    47.     }  
    48.   
    49.     inline static void DelArray(_Ptr p)  
    50.     {  
    51.         if(p) delete [] p;  
    52.     }  
    53. };  
    54. // CDelFunc - 指针删除算法类 结束  
    55.   
    56. //////////////////////////////////////////////////////////////////  
    57.   
    58. // CSmartPtrManager - 智能指针管理类  
    59. class CSmartPtrManager  
    60. {  
    61.     // 内部数据结构  
    62. protected:  
    63.     // 内部指针结构  
    64.     struct _PTR  
    65.     {  
    66.         _Ptr   p_ptr;  
    67.         _DelFun   p_fun;  
    68.         unsigned int u_ref;  
    69.   
    70.         _PTR()  
    71.             : p_ptr(NULL)  
    72.             , p_fun(NULL)  
    73.             , u_ref(0)  
    74.         {  
    75.         }  
    76.   
    77.         _PTR(const _PTR& _ptr)  
    78.             : p_ptr(_ptr.p_ptr)  
    79.             , p_fun(_ptr.p_fun)  
    80.             , u_ref(_ptr.u_ref)  
    81.         {  
    82.         }  
    83.   
    84.         void operator=(const _PTR& _ptr)  
    85.         {  
    86.             this->p_ptr = _ptr.p_ptr;  
    87.             this->p_fun = _ptr.p_fun;  
    88.             this->u_ref = _ptr.u_ref;  
    89.         }  
    90.   
    91.         operator _Ptr()  
    92.         {  
    93.             return this->p_ptr;  
    94.         }  
    95.   
    96.         void DelPtr()  
    97.         {  
    98.             (*(this->p_fun))(this->p_ptr);  
    99.         }  
    100.     };  
    101.     // 内部指针结构 结束  
    102.   
    103.     // 自动回收对象  
    104.     class CAutoRecycle  
    105.     {  
    106.     public:  
    107.         ~CAutoRecycle()  
    108.         {  
    109.             /* 
    110.             当程序退出时可以自动回收 
    111.             所有仍未被回收的指针 
    112.             */  
    113.             CSmartPtrManager::ClrRef();  
    114.         }  
    115.     };  
    116.     // 自动链表对象 结束  
    117.   
    118.     // 友元  
    119. private:  
    120.     friend CAutoRecycle;  
    121.   
    122.     // 成员变量  
    123. protected:  
    124.     static map<_Ptr, _PTR> lst_ptr;  
    125.     static CAutoRecycle   auto_rec;  
    126.   
    127.     // 操作  
    128. public:  
    129.     static void AddRef(_Ptr ptr, _DelFun _del_fun)  
    130.     {  
    131.         map<_Ptr, _PTR>::iterator iter = lst_ptr.find(ptr);  
    132.         if( iter != lst_ptr.end() )  
    133.         {  
    134.             iter->second.u_ref ++ ;  
    135.             return ;  
    136.         }  
    137.         _PTR _ptr;  
    138.         _ptr.p_ptr = ptr;  
    139.         _ptr.p_fun = _del_fun;  
    140.         _ptr.u_ref = 1;  
    141.         lst_ptr.insert( map<_Ptr, _PTR>::value_type(ptr, _ptr) );  
    142.     }  
    143.   
    144.     static void DelRef(_Ptr ptr)  
    145.     {  
    146.         map<_Ptr, _PTR>::iterator iter = lst_ptr.find(ptr);  
    147.         if( iter != lst_ptr.end() )  
    148.         {  
    149.             if( iter->second )  
    150.             {  
    151.                 if( -- iter->second.u_ref ) return ;  
    152.                 iter->second.DelPtr(); // 删除指针  
    153.                 lst_ptr.erase( iter );  
    154.                 return ;  
    155.             }  
    156.         }  
    157.     }  
    158.   
    159. protected:  
    160.   
    161.     static void ClrRef()  
    162.     {  
    163.         // 回收所有指针  
    164.         map<_Ptr, _PTR>::iterator iter = lst_ptr.begin();  
    165.         while( iter != lst_ptr.end() )  
    166.         {  
    167.             _PTR _ptr( iter++->second );  
    168.             if( !_ptr.u_ref ) continue ;  
    169.             _ptr.DelPtr(); // 删除指针  
    170.         }  
    171.         lst_ptr.clear();  
    172.     }  
    173. };  
    174. // CSmartPtrManager - 智能指针管理类 结束  
    175.   
    176. //////////////////////////////////  
    177.   
    178. // 初始化静态变量  
    179. map<_Ptr, CSmartPtrManager::_PTR> CSmartPtrManager::lst_ptr;  
    180. CSmartPtrManager::CAutoRecycle    CSmartPtrManager::auto_rec;  
    181.   
    182. //////////////////////////////////////////////////////////////////  
    183.   
    184. // TSmartPtr - 智能指针类模板  
    185. template<class TYPE, _DelFun DelFunction = CDelFunc::DelSingle>  
    186. class TSmartPtr  
    187. {  
    188.     // 成员变量  
    189. protected:  
    190.     TYPE* m_ptr;  
    191.   
    192.     // 构造/析构  
    193. public:  
    194.     TSmartPtr(void)  
    195.     {  
    196.         m_ptr = NULL;  
    197.     }  
    198.   
    199.     TSmartPtr(TYPE* pt)  
    200.     {  
    201.         (*this) = pt;  
    202.     }  
    203.   
    204.     TSmartPtr(const TSmartPtr& ptr)  
    205.     {  
    206.         (*this) = ptr;  
    207.     }  
    208.   
    209.     virtual ~TSmartPtr(void)  
    210.     {  
    211.         Release();  
    212.     }  
    213.   
    214.     // 操作  
    215. public:  
    216.     void Release()  
    217.     {  
    218.         if( m_ptr )  
    219.         {  
    220.             CSmartPtrManager::DelRef(m_ptr);  
    221.             m_ptr = NULL;  
    222.         }  
    223.     }  
    224.   
    225.     //////////////////////////////////  
    226.   
    227.     void operator=(TYPE* pt)  
    228.     {  
    229.         if( (*this) == pt ) return ;  
    230.         Release();  
    231.         m_ptr = pt;  
    232.         CSmartPtrManager::AddRef(m_ptr, DelFunction);  
    233.     }  
    234.   
    235.     void operator=(const TSmartPtr& ptr)  
    236.     {  
    237.         if( (*this) == ptr ) return ;  
    238.         Release();  
    239.         this->m_ptr = ptr.m_ptr;  
    240.         CSmartPtrManager::AddRef(m_ptr, DelFunction);  
    241.     }  
    242.   
    243.     bool operator==(TYPE* pt) const  
    244.     {  
    245.         return (m_ptr == pt);  
    246.     }  
    247.   
    248.     bool operator==(const TSmartPtr& ptr) const  
    249.     {  
    250.         return (this->m_ptr == ptr.m_ptr);  
    251.     }  
    252.   
    253.     TYPE* operator+(int offset) const  
    254.     {  
    255.         return (m_ptr + offset);  
    256.     }  
    257.   
    258.     TYPE* operator-(int offset) const  
    259.     {  
    260.         return (m_ptr - offset);  
    261.     }  
    262.   
    263.     TYPE* operator->() const  
    264.     {  
    265.         return m_ptr;  
    266.     }  
    267.   
    268.     TYPE& operator*()  
    269.     {  
    270.         return *m_ptr;  
    271.     }  
    272.   
    273.     TYPE& operator[](int inx)  
    274.     {  
    275.         return m_ptr[inx];  
    276.     }  
    277.   
    278.     operator TYPE*() const  
    279.     {  
    280.         return m_ptr;  
    281.     }  
    282. };  
    283. // TSmartPtr - 智能指针类模板 结束  

    使用示例:

    [cpp] view plaincopy
     
    1. TSmartPtr<int> pi = new int;  
    2. (*pi) = 2;  
    3. TSmartPtr<int> pi2 = pi;  
    4. cout << (*pi) << " " << (*pi2) << endl;  
    5. // ...  
    6. TSmartPtr<ClassA> pc = new ClassA;  
    7. pc->FuncA();  
    8. // ...  

    以上的智能指针类解决了包括指针多次复制导致的析构删除错误等问题,利用一个静态的管理类纪录了所有当前存在的指针,每次添加新指针时就在静态指针map中登记,若当前指针已存在就将对应的引用计数加1...

    不过使用这种方法管理引用计数有一个很大的缺点,就是每次添加或删除指针时必须要查找指针map,效率上会有所损失.但是这样做的好处是不会因为将普通指针赋值给智能指针而导致引用计数的管理混乱.

    通常智能指针的解决方案是封装一个用于计数的指针封装类,在智能指针类创建新智能指针时new一个新的计数类并保存它的指针,当出现智能指针之间的拷贝时通过自己保存的计数类指针对计数进行操作,等同于操作了所有拥有这个计数类指针的智能指针的引用计数.这样做就不需要任何查表的操作了,但是不好的地方是一个指针引用计数类的实例并没有严格的与一个指针相绑定...但是只要严格注意使用规范(比如不要使用普通指针直接构造智能指针,在构造新智能指针时一定要使用new出来的新指针),是不会出现指针计数管理出现混乱的错误的.

    在实际使用中,我还发现上面的代码还有因指针型别不同导致指针转换不方便,以及当使用类指针时此智能指针析构函数不会调用其析构函数等一些问题.

    下面是一个改进版的TSmartPtr:

    [cpp] view plaincopy
     
    1. //////////////////////////////////////////////////////////////////  
    2. // TSmartPtr - 智能指针类模板  
    3. //  
    4. // Author:  木头云  
    5. // Blog:    http://hi.baidu.com/markl22222  
    6. // E-Mail:  mark.lonr@tom.com  
    7. // Version: 1.1.831.1200(2009/08/31)  
    8. //  
    9. // History:  
    10. //  - 1.0.813.1148(2009/08/13)  @ 完成基本的类模板构建  
    11. //  - 1.0.814.1758(2009/08/14)  + 添加TSmartPtr类相关操作符重载(==)  
    12. //                              + 添加CSmartPtrManager智能指针管理类及全局的智能指针管理链表  
    13. //  - 1.0.817.1508(2009/08/17)  ^ 优化CSmartPtrManager使之支持引用计数  
    14. //                              + 添加TSmartPtr类相关操作符重载(*)  
    15. //  - 1.0.818.1600(2009/08/18)  ^ 优化CSmartPtrManager指针管理链表的list为map  
    16. //                              - 删除CSmartPtrManager类的Find()函数  
    17. //                              = 调整TSmartPtr类模板参数,将is_ary改为DelFunction函数指针  
    18. //                              = 调整CSmartPtrManager类,将其函数改为静态函数,并定义自动回收对象CAutoRecycle  
    19. //                              + 添加CDelFunc类,用于统一管理指针删除算法  
    20. //  - 1.0.819.1720(2009/08/19)  + 添加TSmartPtr类相关操作符重载([],+,-)  
    21. //                              = 调整TSmartPtr类=操作符的返回值为TSmartPtr&  
    22. //                              # 修正当指针自我复制时导致的指针被销毁的bug  
    23. //  - 1.1.820.2300(2009/08/20)  + 添加TSmartPtr类相关成员函数对不同型别指针的支持  
    24. //                              - 删除CSmartPtrManager管理类,让TSmartPtr类使用TPtr指针计数类指针自动管理指针计数  
    25. //                              = 调整_Ptr及_DelFun类型重定义到CDelFunc类内部  
    26. //                              ^ 优化TSmartPtr(TYPE*)构造函数的执行效率  
    27. //                              = 调整类及其成员函数的声明格式  
    28. //  - 1.1.821.1645(2009/08/21)  + 添加TPtr类相关操作符重载(!=)  
    29. //                              + 添加TSmartPtr类相关操作符重载(!=)  
    30. //  - 1.1.825.1255(2009/08/25)  # 修正TSmartPtr类bool operator==()与bool operator!=()对NULL指针的判断错误  
    31. //  - 1.1.827.1353(2009/08/27)  = 调整TPtr与TSmartPtr类模板TYPE参数的默认值为CDelFunc::_Ptr  
    32. //  - 1.1.831.1200(2009/08/31)  # 修正当使用类指针,智能指针析构时不会调用其析构函数的bug  
    33. //////////////////////////////////////////////////////////////////  
    34.   
    35. #ifndef __STDCPX_SMARTPTR_H__  
    36. #define __STDCPX_SMARTPTR_H__  
    37.   
    38. #pragma once  
    39.   
    40. //////////////////////////////////////////////////////////////////  
    41.   
    42. // 指针类型重定义  
    43. typedef void* _Ptr;  
    44.   
    45. //////////////////////////////////////////////////////////////////  
    46.   
    47. // TPtr - 指针计数类 声明  
    48. template<class TYPE = _Ptr, bool ARRAY = false>  
    49. class TPtr;  
    50.   
    51. // TSmartPtr - 智能指针类模板 声明  
    52. template<class TYPE = _Ptr, bool ARRAY = false>  
    53. class TSmartPtr;  
    54.   
    55. //////////////////////////////////////////////////////////////////  
    56.   
    57. // 指针计数类  
    58. template<class TYPE, bool ARRAY>  
    59. class TPtr  
    60. {  
    61. // 友元  
    62. protected:  
    63.     friend class TSmartPtr<TYPE, ARRAY>;  
    64.   
    65. // 成员变量  
    66. protected:  
    67.     TYPE*        p_ptr;  
    68.     unsigned int u_ref;  
    69.   
    70. // 构造/析构  
    71. protected:  
    72.     TPtr()  
    73.         : p_ptr(NULL)  
    74.         , u_ref(0)  
    75.     {  
    76.     }  
    77.   
    78.     explicit TPtr(TYPE* pt)  
    79.         : p_ptr(pt)  
    80.         , u_ref(1)  
    81.     {  
    82.     }  
    83.   
    84.     virtual ~TPtr()  
    85.     {  
    86.         // 调用合适的清理函数  
    87.         if( ARRAY )  
    88.             delete [] p_ptr;  
    89.         else  
    90.             delete p_ptr;  
    91.     }  
    92.   
    93. // 操作  
    94. protected:  
    95.     unsigned int GetRefCount()  
    96.     {  
    97.         return u_ref;  
    98.     }  
    99.   
    100.     TYPE* GetPtr()  
    101.     {  
    102.         return p_ptr;  
    103.     }  
    104.   
    105.     //////////////////////////////////  
    106.   
    107.     bool operator==(TYPE* pt) const  
    108.     {  
    109.         return (p_ptr == pt);  
    110.     }  
    111.   
    112.     bool operator!=(TYPE* pt) const  
    113.     {  
    114.         return (p_ptr != pt);  
    115.     }  
    116.   
    117.     void operator++()  
    118.     {  
    119.         ++ u_ref;  
    120.     }  
    121.   
    122.     void operator--()  
    123.     {  
    124.         if( -- u_ref )  
    125.         {  
    126.             return ;  
    127.         }  
    128.         delete this;  
    129.     }  
    130.   
    131.     TYPE& operator*()  
    132.     {  
    133.         return *p_ptr;  
    134.     }  
    135.   
    136.     operator TYPE*()  
    137.     {  
    138.         return p_ptr;  
    139.     }  
    140. };  
    141. // 指针计数类 结束  
    142.   
    143. //////////////////////////////////////////////////////////////////  
    144.   
    145. // TSmartPtr - 智能指针类模板  
    146. template<class TYPE, bool ARRAY>  
    147. class TSmartPtr  
    148. {  
    149. // 友元  
    150. protected:  
    151.     template<class TYPE2, bool ARRAY>  
    152.     friend class TSmartPtr;  
    153.   
    154. // 成员变量  
    155. protected:  
    156.     TPtr<TYPE, ARRAY>* m_ptr;  
    157.   
    158. // 构造/析构  
    159. public:  
    160.     TSmartPtr(void)  
    161.         : m_ptr(NULL)  
    162.     {  
    163.     }  
    164.   
    165.     TSmartPtr(TYPE* pt)  
    166.         : m_ptr(NULL)  
    167.     {  
    168.         if( !pt ) return ;  
    169.         m_ptr = new TPtr<TYPE, ARRAY>(pt);  
    170.     }  
    171.   
    172.     TSmartPtr(const TSmartPtr<TYPE>& ptr)  
    173.         : m_ptr(NULL)  
    174.     {  
    175.         (*this) = ptr;  
    176.     }  
    177.   
    178.     //////////////////////////////////  
    179.   
    180.     template<class TYPE2>  
    181.     TSmartPtr(TYPE2* pt)  
    182.         : m_ptr(NULL)  
    183.     {  
    184.         if( !pt ) return ;  
    185.         m_ptr = new TPtr<TYPE, ARRAY>((TYPE*)pt);  
    186.     }  
    187.   
    188.     template<class TYPE2>  
    189.     TSmartPtr(const TSmartPtr<TYPE2>& ptr)  
    190.         : m_ptr(NULL)  
    191.     {  
    192.         (*this) = ptr;  
    193.     }  
    194.   
    195.     //////////////////////////////////  
    196.   
    197.     virtual ~TSmartPtr(void)  
    198.     {  
    199.         if( m_ptr ) -- (*m_ptr);  
    200.     }  
    201.   
    202. // 操作  
    203. public:  
    204.     unsigned int GetRefCount()  
    205.     {  
    206.         if( m_ptr )  
    207.             return m_ptr->GetRefCount();  
    208.         else  
    209.             return 0;  
    210.     }  
    211.   
    212.     void Release()  
    213.     {  
    214.         if( m_ptr ) delete m_ptr;  
    215.         m_ptr = NULL;  
    216.     }  
    217.   
    218.     //////////////////////////////////  
    219.   
    220.     TSmartPtr<TYPE, ARRAY>& operator=(const TSmartPtr<TYPE, ARRAY>& ptr)  
    221.     {  
    222.         if( (*this) == ptr ) return (*this);  
    223.         if( m_ptr ) -- (*m_ptr);  
    224.         m_ptr = ptr.m_ptr;  
    225.         if( m_ptr ) ++ (*m_ptr);  
    226.         return (*this);  
    227.     }  
    228.   
    229.     bool operator==(TYPE* pt) const  
    230.     {  
    231.         if( m_ptr )  
    232.             return ((*m_ptr) == (TYPE*)pt);  
    233.         else if( !pt )  
    234.             return true;  
    235.         else  
    236.             return false;  
    237.     }  
    238.   
    239.     bool operator==(const TSmartPtr<TYPE, ARRAY>& ptr) const  
    240.     {  
    241.         return (m_ptr == (TPtr<TYPE>*)ptr);  
    242.     }  
    243.   
    244.     bool operator!=(TYPE* pt) const  
    245.     {  
    246.         if( m_ptr )  
    247.             return ((*m_ptr) != (TYPE*)pt);  
    248.         else if( pt )  
    249.             return true;  
    250.         else  
    251.             return false;  
    252.     }  
    253.   
    254.     bool operator!=(const TSmartPtr<TYPE, ARRAY>& ptr) const  
    255.     {  
    256.         return (m_ptr != (TPtr<TYPE, ARRAY>*)ptr);  
    257.     }  
    258.   
    259.     TYPE* operator+(int offset) const  
    260.     {  
    261.         return (m_ptr + offset);  
    262.     }  
    263.   
    264.     TYPE* operator-(int offset) const  
    265.     {  
    266.         return (m_ptr - offset);  
    267.     }  
    268.   
    269.     TYPE* operator->() const  
    270.     {  
    271.         return m_ptr->GetPtr();  
    272.     }  
    273.   
    274.     TYPE& operator*()  
    275.     {  
    276.         return *(*m_ptr);  
    277.     }  
    278.   
    279.     TYPE& operator[](int inx)  
    280.     {  
    281.         return (m_ptr->GetPtr())[inx];  
    282.     }  
    283.   
    284.     operator TYPE*() const  
    285.     {  
    286.         return m_ptr->GetPtr();  
    287.     }  
    288.   
    289.     //////////////////////////////////  
    290.   
    291.     template<class TYPE2>  
    292.     TSmartPtr<TYPE, ARRAY>& operator=(const TSmartPtr<TYPE2, ARRAY>& ptr)  
    293.     {  
    294.         if( (*this) == ptr ) return (*this);  
    295.         if( m_ptr ) -- (*m_ptr);  
    296.         m_ptr = (TPtr<TYPE, ARRAY>*)(TPtr<TYPE2, ARRAY>*)ptr;  
    297.         if( m_ptr ) ++ (*m_ptr);  
    298.         return (*this);  
    299.     }  
    300.   
    301.     template<class TYPE2>  
    302.     bool operator==(TYPE2* pt) const  
    303.     {  
    304.         return ((*this) == (TYPE*)pt);  
    305.     }  
    306.   
    307.     template<class TYPE2>  
    308.     bool operator==(const TSmartPtr<TYPE2, ARRAY>& ptr) const  
    309.     {  
    310.         return (m_ptr == (TPtr<TYPE, ARRAY>*)(TPtr<TYPE2, ARRAY>*)ptr);  
    311.     }  
    312.   
    313.     template<class TYPE2>  
    314.     bool operator!=(TYPE2* pt) const  
    315.     {  
    316.         return ((*this) != (TYPE*)pt);  
    317.     }  
    318.   
    319.     template<class TYPE2>  
    320.     bool operator!=(const TSmartPtr<TYPE2, ARRAY>& ptr) const  
    321.     {  
    322.         return (m_ptr != (TPtr<TYPE, ARRAY>*)(TPtr<TYPE2, ARRAY>*)ptr);  
    323.     }  
    324.   
    325.     template<class TYPE2>  
    326.     operator TYPE2*() const  
    327.     {  
    328.         return (TYPE2*)(m_ptr->GetPtr());  
    329.     }  
    330.   
    331.     //////////////////////////////////  
    332.   
    333.     protected:  
    334.     operator TPtr<TYPE, ARRAY>*() const  
    335.     {  
    336.         return m_ptr;  
    337.     }  
    338. };  
    339. // TSmartPtr - 智能指针类模板 结束  
    340.   
    341. //////////////////////////////////////////////////////////////////  
    342.   
    343. #endif  // __STDCPX_SMARTPTR_H__  

    现在的智能指针不再使用map存储指针表了,另外加上了指针型别不同的拷贝,转换支持.其使用方法没有很大的变化,但是要注意一定不能这样使用:

    [cpp] view plaincopy
     
    1. TSmartPtr<int> pi = new int;  
    2. int* pint = (int*)pi;  
    3. TSmartPtr<int> pj = pint; // 此处这样使用会在pj析构时将pint删除两次  

    而原来的代码版本即使这样使用也不会出问题.

    以上代码在Microsoft Visual C++ 2005上编译通过.

    http://blog.csdn.net/markl22222/article/details/5308111

  • 相关阅读:
    DRF__序列化(1)serializers.Serializer 基本的序列化、反序列化概念
    小整数池以及字符串驻留机制
    Paginator实现分页
    xml代码 解决eclipse乱码问题
    css,解决文字与图片对齐的问题
    鼠标悬停,背景颜色变化问题
    解决html代码文本复制问题,js技术
    解决 点击超链接后,周围出现的虚线框的问题
    将一个html文件引入另一个html文件的div中
    WebStorm出现中文乱码解决代码
  • 原文地址:https://www.cnblogs.com/findumars/p/5017761.html
Copyright © 2020-2023  润新知