shared_ptr的手写实现


template <typename T>
class SmartPointer {
    
    
public:
    //构造函数
    SmartPointer(T* p=0): _ptr(p), _reference_count(new size_t){
    
    
        if(p)
            *_reference_count = 1;
        else
            *_reference_count = 0;
    }
    //拷贝构造函数
    SmartPointer(const SmartPointer& src) {
    
    
        if(this!=&src) {
    
    
            _ptr = src._ptr;
            _reference_count = src._reference_count;
            (*_reference_count)++;
        }
    }
    //重载赋值操作符
    SmartPointer& operator=(const SmartPointer& src) {
    
    
        if(_ptr==src._ptr) {
    
    
            return *this;
        }
        releaseCount();
        _ptr = src._ptr;
        _reference_count = src._reference_count;
        (*_reference_count)++;
        return *this;
    }
     
    //重载操作符
    T& operator*() {
    
    
        if(ptr) {
    
    
            return *_ptr;
        }
        //throw exception
    }
    //重载操作符
    T* operator->() {
    
    
        if(ptr) {
    
    
            return _ptr;
        }
        //throw exception
    }
    //析构函数
    ~SmartPointer() {
    
    
        if (--(*_reference_count) == 0) {
    
    
            delete _ptr;
            delete _reference_count;
        }
    }
private:
   T *_ptr;
   size_t *_reference_count;
   void releaseCount() {
    
    
        if(_ptr) {
    
    
            (*_reference_count)--;
            if((*_reference_count)==0) {
    
    
                delete _ptr;
                delete _reference_count;
            }
        }
    }
};
 

猜你喜欢

转载自blog.csdn.net/u014618114/article/details/107989797