zoukankan      html  css  js  c++  java
  • 智能指针-----实现

    unique_ptr:

      https://blog.csdn.net/afei__/article/details/80670283

    最简单的智能指针实现:

    #include <numeric>
    #include <array>
    #include <cstring>
    #include <cstdio>
    #include <windows.h>
    using namespace  std;
    template <class T>
    class smartPoint
    {
    private:
        T* m_pointer;
    public:
        smartPoint(T*pp = NULL){ m_pointer = pp; }
        T* operator->(){ return m_pointer; }
        T&  operator*(){ return *m_pointer; }
        ~smartPoint(){ delete m_pointer; }
        smartPoint(const smartPoint<T>& obj)
    {
    m_pointer = obj.m_pointer; const_cast<smartPoint<T>& >(obj).m_pointer = NULL;
      }
    smartPoint
    <T>& operator=(const smartPoint<T>& obj) { if (obj != this) { delete m_pointer;
           m_pointer=obj.m_pointer; const_cast
    <smartPoint<T>&>(obj).m_pointer = NULL; } return *this; } } };

    share_ptr:https://blog.csdn.net/k346k346/article/details/81478223

    实现参考:https://www.cnblogs.com/cycxtz/p/4742970.html

    #include <algorithm>
    #include <iostream>
    #include <functional>
    #include <vector>
    #include <numeric>
    #include <array>
    #include <cstring>
    #include <cstdio>
    #include <windows.h>
    using namespace  std;
    class Point
    {
    private:
    	int x, y;
    public:
    	Point(int xVal = 0, int yVal = 0) :x(xVal), y(yVal) { }
    	int getX() const { return x; }
    	int getY() const { return y; }
    	void setX(int xVal) { x = xVal; }
    	void setY(int yVal) { y = yVal; }
    };
    
    template<class T>
    class share_ptr{
    private:
    	int* count;//用于引用计数
    	T* data;
    public:
    	share_ptr(T* data) :data(data), count(new int(1)){}//构造函数,初始化
    	share_ptr(const share_ptr<T>& rp){
    		data = rp.data;
    		count = rp.count;
    		(*count)++; }
    	share_ptr<T>& operator=(const share_ptr<T>& rp)
    	{ 
    		if (rp.data==data) return *this
    		else
    		{ 
    			if (*count == 1)
    			{
    				delete data,data=NULL;
    				delete count,count=NULL;
    			}
    			data = rp.data;
    			count = rp.count;
    			--*count;
    			++*rp.count;
    		}
    		return *this;
    		
    	}
    	T* operator->(){ return data; }
    	T& operator*(){ return *data; }
    	~share_ptr(){//count为1,直接释放,否则count-- 
    		if(*count==1) delete data,delete count;
    		else --*count ,cout << "还有:" << *count << "指向当前对象"<<endl;
    	}
    
    };
    
    int main()
    {
    	//定义一个基础对象类指针
    	Point *pa = new Point(10, 20);
    
    	//定义三个智能指针类对象,对象都指向基础类对象pa
    	//使用花括号控制三个智能指针的生命周期,观察计数的变化
    	{
    		share_ptr<Point> sptr1(pa);//此时计数count=1
    		cout << "sptr1:" << sptr1->getX() << "," << sptr1->getY() << endl;
    		{
    			share_ptr<Point> sptr2(sptr1); //调用拷贝构造函数,此时计数为count=2
    			cout << "sptr2:" << sptr2->getX() << "," << sptr2->getY() << endl;
    			{
    				share_ptr<Point> sptr3 = sptr1; //调用赋值操作符,此时计数为conut=3
    				cout << "sptr3:" << (*sptr3).getX() << "," << (*sptr3).getY() << endl;
    			}
    			//此时count=2
    		}
    		//此时count=1;
    	}
    	//此时count=0;pa对象被delete掉
    	cout << pa->getX() << endl;
    	system("pause");
    	return 0;
    }
    

     执行结果:

    share_ptr与weak_Ptr:https://www.cnblogs.com/wangkeqin/p/9351191.html

    https://www.cnblogs.com/curo0119/p/8919990.html

    注意事项:https://blog.csdn.net/zhubaohua_bupt/article/details/81430342

    weak_ptr:https://www.cnblogs.com/blog-yejy/archive/2018/09/30/9727070.html

  • 相关阅读:
    linux 内核配置
    使用 git 下载linux 源码
    订阅 linux 邮件列表注意的问题
    使用反射创建一维数组和二维数组
    反射API
    反射机制
    集合案例--对ArrayList容器中的内容进行排序
    Collections
    TreeSet
    Set容器
  • 原文地址:https://www.cnblogs.com/bwbfight/p/11312551.html
Copyright © 2011-2022 走看看