VC语言

本类阅读TOP10

·VC++ 学习笔记(二)
·用Visual C++打造IE浏览器(1)
·每个开发人员现在应该下载的十种必备工具
·教你用VC6做QQ对对碰外挂程序
·Netmsg 局域网聊天程序
·Windows消息大全
·VC++下使用ADO编写数据库程序
·VC++学习笔记(四)
·非法探取密码的原理及其防范
·怎样在VC++中访问、修改注册表

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
一个智能指针的实现(代码)

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

File: shared_ptr.h

///////////////////////////////////////////////////////////////////////////

//

//            template class shared_ptr

//            2004-6-12

//            Chengliang Shan

//            [email protected]

//

///////////////////////////////////////////////////////////////////////////

#ifndef SHARED_PTR_H_

#define SHARED_PTR_H_

 

namespace clshan{

 

       template<class T>

       class shared_ptr;

 

       template<class T>

       class shared_data

       {

       private:

              friend class shared_ptr<T>;

 

              explicit shared_data(T* pT):_M_ptr(pT) {

                     _M_nCount = 1;

              }

 

              ~shared_data() {

                     delete _M_ptr;

              }

             

              void operator++ () {

                     ++_M_nCount;

              }

             

              operator-- () {

                     --_M_nCount;

                     if (_M_nCount == 0) {

                            delete this;

                     }

              }

 

              T&  operator* () const {

                     return *_M_ptr;

              }

 

              T*   operator-> () const {

                     return _M_ptr;

              }

 

              bool operator== (T* pT) const {

                     return _M_ptr == pT;

              }

             

              T*   get() {

                     return _M_ptr;

              }

 

              int use_count() {

                     return _M_nCount;

              }

 

       private:

              T*   _M_ptr;

              unsigned int    _M_nCount;

       };

 

       template<class T>

       class shared_ptr

       {

              typedef shared_data<T> element;

       public:

              explicit shared_ptr(T* pT):_M_pD(NULL) {

                     _M_pD = new element(pT);

              }

 

              explicit shared_ptr():_M_pD(NULL){

              }

 

              // copy constructor

              shared_ptr(const shared_ptr<T>& rT) {

                     _M_pD = rT.get_element();

                     if (_M_pD != NULL) {

                            ++(*_M_pD);

                     }

              }

 

              ~shared_ptr() {

                     if (_M_pD != NULL) {

                            --(*_M_pD);

                     }

              }

 

              // assignment operator

              shared_ptr<T>& operator = (shared_ptr<T>& rT) {

                     if (_M_pD != NULL) {

                            --(*_M_pD);

                     }

                     _M_pD = rT.get_element();

                     if (_M_pD != NULL){

                            ++(*_M_pD);

                     }

                     return *this;

              }

 

              T&  operator* () const {

                     return _M_pD->operator *();

              }

 

              T*   operator-> () const {

                     return _M_pD->operator ->();

              }

 

              bool operator== (shared_ptr<T>& rT) const {

                     return rT.get_element() == _M_pD;

              }

             

              bool operator== (T* pT) const {

                     if (_M_pD == NULL){

                            return pT == NULL;

                     }

                     return *_M_pD == pT;

              }

             

              T* get() {

                     if (_M_pD == NULL) {

                            return NULL;

                     }

                     else {

                            return _M_pD->get();

                     }

              }

      

              void release() {

                     if (_M_pD != NULL) {

                            --(*_M_pD);

                            _M_pD = NULL;

                     }                  

              }

             

              void reset(T* pT) {

                     if (_M_pD != NULL) {

                            --(*_M_pD);

                            _M_pD = NULL;

                     }

                     _M_pD = new element(pT);

              }

 

       private:

              element* get_element()const  {

                     return _M_pD;

              }

 

              element* _M_pD;

       };

}

 

#endif




相关文章

相关软件