C++ 智能指针shared_ptr模板实现原理

C++ 智能指针shared_ptr通过引用计数来管理指向的对象,不需要人工释放

这篇博文主要是讲解了智能指针的实现原理,怎么实现引用计数

#include <iostream>

using namespace std;

namespace shao_smart_point{

	//智能指针内部引用计数类实现
	class smart_cout{
	private:
		int use_count;
	public:
		smart_cout():use_count(0){
		
		}

		smart_cout(int count):use_count(count){
		
		}

		int addRef(){
			++use_count;
			return use_count;
		}

		int releaseRef(){
			--use_count;
			return use_count;
		}

		int getCount(){
			return use_count;
		}

	};

	//智能指针实现原理
	template<class T>
	class smart_point{
	
	  private:
		  T* pref;
		  smart_cout* smartCount;


	  public:
		  //构造函数
		  smart_point():pref(nullptr),smartCount(nullptr){
		  
		  }

		  //构造函数
		  smart_point(T*data):pref(data),smartCount(new smart_cout()){
			  if(data != nullptr && smartCount != nullptr){
				  smartCount->addRef(); 
			  }
		  }

		  //拷贝构造
		  smart_point(const smart_point<T>& smartPoint){
			  //进行浅拷贝
			  this->pref = smartPoint.pref;
			  this->smartCount = smartPoint.smartCount;
		       
			  //引用加1
			  if(this->smartCount != nullptr){
				  this->smartCount->addRef();
			  }
		  }

		  //赋值重载实现
		  void operator=(const smart_point<T>& smartPoint){

			  //清理自己之前的指向
			  if(this->pref != nullptr && this->smartCount->releaseRef() <= 0){
				  delete this->pref;
				  delete this->smartCount;
				  this->pref = nullptr;
			  }

			  //浅拷贝
			  this->pref =  smartPoint.pref;
			  this->smartCount = smartPoint.smartCount;
			  this->smartCount->addRef();
		  
		  }

		  //重新指向
		  void reset(T * data){

			  if(this->pref != nullptr && this->smartCount->releaseRef() <= 0){
				  delete this->pref;
				  delete this->smartCount;
				  this->pref = nullptr;
			  }

			  this->pref = data;
			  if(this->pref != nullptr){

			    this->smartCount = new smart_cout(1);
			  }else{

				  this->smartCount = nullptr;
			  }
		  
		  }

		  void reset(const smart_point<T>& smartPoint){

			  if(this->pref != nullptr && this->smartCount->releaseRef() <= 0){
				  delete this->pref;
				  delete this->smartCount;
				  this->pref = nullptr;
			  }

			  this->pref = smartPoint.pref;
			  this->smartCount = smartPoint.smartCount;
			  if(this->pref != nullptr){
				  this->smartCount->addRef();
			  }
		  
		  }

		  bool operator ==(const smart_point<T>& smartPoint){
			  return this->pref == smartPoint.pref;
		  }


		  T& operator*(){
			  return *pref;
		  }


		  T* get(){
		     return pref;
		  }
		  

		  int useCount(){
			  if(smartCount != nullptr){
				  return smartCount->getCount();
			  }
		  }


		  //析构函数
		  ~smart_point(){
			  if(pref != nullptr && smartCount->releaseRef() <= 0){
			    //释放指针
                delete pref;
				delete smartCount;
				pref = nullptr;
			    cout << "delete" << endl;
			  }
		    
		  }

	};


}


void test(){
	
	shao_smart_point::smart_point<int> mSmart(new int);
	shao_smart_point::smart_point<int> mSmart2(mSmart);
	mSmart2.reset(new int);
	
	cout << mSmart.useCount() << endl;
	cout << mSmart2.useCount() << endl;

}


void main(){

	test();
    cin.get();
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值