手写一个sharedPointer

	class SharedCount
	{
	public:
    	SharedCount() : count_{1} {}

    	void add() { ++count_; }

    	void minus() { --count_; }

    	int get() const { return count_; }

    private:
    	std::atomic<int> count_;
	};

	template<typename T> class SharedPtr
	{
	public:
		SharedPtr(void) : ptr_(nullptr), ref_count_(nullptr) {}
		SharedPtr(T* ptr) : ptr_(ptr), ref_count_(new SharedCount) {}

		~SharedPtr(void) { clean(); }

		SharedPtr(const SharedPtr& p)
		{
			if (this == &p) {
				return;
			}
			this->ptr_ = p.ptr_;
			this->ref_count_ = p.ref_count_;
			ref_count_->add();
		}

		SharedPtr& operator=(const SharedPtr& p)
		{
			if (this->ptr_ == p.ptr_ ) {
				return *this;
			}

        	clean();  // 疑似有误
        	this->ptr_ = p.ptr_;
        	this->ref_count_ = p.ref_count_;
        	ref_count_->add();
        	return *this;
    	}

    	SharedPtr(SharedPtr&& p)
    	{
        	this->ptr_ = p.ptr_;
        	this->ref_count_ = p.ref_count_;
        	p.ptr_ = nullptr;
        	p.ref_count_ = nullptr;
    	}

    	SharedPtr& operator=(SharedPtr&& p)
    	{
        	clean();
        	this->ptr_ = p.ptr_;
        	this->ref_count_ = p.ref_count_;
        	p.ptr_ = nullptr;
        	p.ref_count_ = nullptr;
        	return *this;
    	}

    	int use_count() { return ref_count_->get(); }

		T* get() const { return ptr_; }

		T* operator->(void) const { return ptr_; }

		T const* operator->(void) const {return ptr_;}

		T& operator*(void) const { return *ptr_; } 
		
		T const& operator*(void) const { return *ptr_; }

		operator bool() const { return ptr_ != nullptr; }

		bool isValid() const { return nullptr != ptr_; }
		bool isNull(void) const { return !isValid(); }
		void setNull(void);
		void swap(SharedPtr& rhs)
		{
			T* temp = ptr_;
			ptr_ = rhs.ptr_;
			rhs.ptr_ = temp;
		}

		bool operator ==(T* rhs) const {return ptr_ == rhs;}
		bool operator !=(T* rhs) const {return ptr_ != rhs;}
		bool operator ==(T const* rhs) const;
		bool operator !=(T const* rhs) const;
		bool operator ==(SharedPtr<T> const& rhs) const { return ptr_ == rhs.ptr_; }
		bool operator !=(SharedPtr<T> const& rhs) const { return ptr_ != rhs.ptr_; }

		SharedPtr<T>& operator =(T* rhs);
		SharedPtr<T>& operator =(SharedPtr<T>& rhs);

		//RELATED TYPE convert
		template<typename U> SharedPtr<T>& operator =(U*);

		template<typename U> SharedPtr<T>& operator =(const SharedPtr<U>& rhs)
		{
			SharedPtr(rhs).swap(*this);
			return *this;
		}

		template<typename U> explicit operator U* (void) const;

	private:
		void clean()
		{
			if (ref_count_) {
				ref_count_->minus();
				if (ref_count_->get() == 0) {
					if (ptr_) {
						delete ptr_;
						ptr_ = nullptr;
					}
					delete ref_count_;
					ref_count_ = nullptr;
				}
			}
		}

		T* ptr_;
		SharedCount* ref_count_;
	};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值