一个智能指针的实现(代码)

///

//

//            template class shared_ptr

//            2004-6-12

//            Chengliang Shan

//            simouse@126.com

//

///

#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

 
### 回答1: 好的,以下是一个简单的C++智能指针实现代码示例: ``` #include <iostream> template <typename T> class SmartPointer { public: explicit SmartPointer(T* ptr = nullptr) : m_ptr(ptr) {} ~SmartPointer() { delete m_ptr; } // 禁止拷贝构造函数和赋值运算符 SmartPointer(const SmartPointer&) = delete; SmartPointer& operator=(const SmartPointer&) = delete; // 获取指针 T* get() const { return m_ptr; } // 重载解引用运算符 T& operator*() const { return *m_ptr; } // 重载箭头运算符 T* operator->() const { return m_ptr; } // 重载布尔运算符,用于判断是否为nullptr operator bool() const { return m_ptr != nullptr; } private: T* m_ptr; }; int main() { SmartPointer<int> p(new int(42)); std::cout << *p << std::endl; std::cout << p->size() << std::endl; if (p) { std::cout << "p is not null." << std::endl; } return 0; } ``` 这个智能指针类的实现中,通过封装原始指针来实现自动释放内存的功能。当智能指针被销毁时,其内部保存的指针会被自动释放,从而避免了内存泄漏的问题。此外,该实现还禁止了拷贝构造函数和赋值运算符的使用,以避免多个智能指针指向同一个内存块的问题。 ### 回答2: 以下是一个简单的C++智能指针实现示例代码: ```cpp #include <iostream> template <typename T> class SmartPointer { public: SmartPointer(T* ptr) : ptr_(ptr) {} ~SmartPointer() { delete ptr_; } T& operator*() const { return *ptr_; } T* operator->() const { return ptr_; } private: T* ptr_; }; int main() { SmartPointer<int> sp(new int(5)); std::cout << *sp << std::endl; // 输出 5 std::cout << sp->get() << std::endl; // 输出 5 return 0; } ``` 这段代码中,我们定义了一个名为SmartPointer的模板类,用于实现智能指针。它接受一个指向任意类型对象的原始指针作为构造函数参数,并负责该指针对象的内存管理。 在类的实现中,我们重载了解引用运算符*和箭头运算符->,使得智能指针的使用方式与原始指针类似。在类的析构函数中,我们释放了指向的内存。 在main函数中,我们创建了一个int类型对象的智能指针sp,并将其包装在SmartPointer类中。我们可以通过*和->运算符来访问所指向的对象,就像原始指针一样。最后,我们通过输出流输出对象的值。 这个示例是一个简单的智能指针实现,仅用于演示基本原理。在实际应用中,我们可能需要更复杂的实现,例如引用计数、拷贝控制等机制,以确保内存的正确管理和多线程安全。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值