zoukankan      html  css  js  c++  java
  • 共享指针的简单实现

    共享指针的简单实现,主要参考 https://www.cnblogs.com/xiehongfeng100/p/4645555.html

    // Study.cpp: 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include <iostream>
    #include <vector>
    #include <unordered_map>
    #include <unordered_set>
    #include <queue>
    #include <string>
    #include <algorithm>
    #include <sstream>
    using namespace std;
    
    
    template<class T>
    class SmartPtr
    {
    public:
    	SmartPtr(T *p);
    	~SmartPtr();
    	SmartPtr(const SmartPtr<T> &orig);                // 浅拷贝
    	SmartPtr<T>& operator=(const SmartPtr<T> &rhs);    // 浅拷贝
    	void getcount()
    	{
    		cout << *use_count << " ";
    	}
    private:
    	T * ptr;
    	// 将use_count声明成指针是为了方便对其的递增或递减操作
    	int *use_count;
    };
    
    template<class T>
    SmartPtr<T>::SmartPtr(T *p):ptr(p)
    {
    	use_count = new int(1);
    	if (use_count == nullptr)
    	{
    		p = nullptr;
    		cout << " Allocation Error " << endl;
    		return;
    	}
    
    }
    
    template<class T>
    SmartPtr<T>::~SmartPtr()
    {
    	if (--(*use_count) == 0)
    	{
    		delete ptr;
    		ptr = nullptr;
    		delete use_count;
    		use_count = nullptr;
    	}
    }
    template<class T>
    SmartPtr<T>::SmartPtr(const SmartPtr<T> &orig)
    {
    	ptr = orig.ptr;
    	use_count = orig.use_count;
    	++(*use_count);
    }
    template<class T>
    SmartPtr<T>& SmartPtr<T>::operator=(const SmartPtr<T> &rhs)
    {
    	if (this == &rhs)
    		return *this;
    
    	this->~SmartPtr();
    
    	use_count = rhs.use_count;
    	++(*use_count);
    	ptr = rhs.ptr;
    
    };
    int main()
    {
    	string* p1 = new string("hello");
    	SmartPtr<string> sp1(p1);
    	string* p2 = new string("hello");
    	SmartPtr<string> sp2(p2);
    	vector<SmartPtr<string>> vp;
    	vp.push_back(sp1);
    	vp.push_back(sp2);
    	vp.push_back(sp2);
    	vp.push_back(sp1);
    	cout << "sp1   sp2  :  "; sp1.getcount(); sp2.getcount(); cout << endl;
    
    	vp.pop_back();
    	vp.pop_back();
    	cout << "sp1   sp2  :  "; sp1.getcount(); sp2.getcount(); cout << endl;
    
    
    	SmartPtr<string> sp3(sp1);
    	cout << "sp1   sp2  :  "; sp1.getcount(); sp2.getcount(); cout << endl;
    
    	sp2 = sp2;
    
    	cout << "sp1   sp2  :  "; sp1.getcount(); sp2.getcount(); cout << endl;
    
    	sp3 = sp2;
    
    	cout << "sp1   sp2  :  "; sp1.getcount(); sp2.getcount(); cout << endl;
    	//while (1)
    	//{
    	//	cin >> a >> b;
    	//	cout << ((a & b) + ((a ^ b) >> 1)) << endl;
    	//}
    
    	system("pause");
    	return 0;
    }
    

      

  • 相关阅读:
    matlab练习程序(RGB2HSV、HSV2RGB)
    matlab练习程序(距离变换)
    C++生成xml(使用tinyxml)
    matlab练习程序(RGB2YUV、YUV2RGB)
    修改 Google Desktop 的缓存目录
    Access 2003 中 null 和 '' 空字符串的奇怪问题
    娃娃
    【js:片断】jQuery 设置 select 下拉框的选中状态
    由 TypeInitializationException 引起的问题
    afaca 分析报告
  • 原文地址:https://www.cnblogs.com/Oscar67/p/9565773.html
Copyright © 2011-2022 走看看