zoukankan      html  css  js  c++  java
  • Interview_C++_day11

    摸鱼的一天。。

    (shared\_ptr) 指针的实现

    template<typename T> class Shared_ptr {
    private:
    	T *ptr;
    	int *use_count;	// 用指针保证指向同一块地址
    public:
    	Shared_ptr() {
    		ptr = nullptr;
    		use_count = nullptr;
    		cout << "Created" << endl;
    	}
    	Shared_ptr(T *p){
    		ptr = p;
    		use_count = new int(1);
    		cout << "Created" << endl;
    	}
    	Shared_ptr(const Shared_ptr<T> &other) {
    		ptr = other.ptr;
    		++(*other.use_count);
    		use_count = other.use_count;
    		cout << "Created" << endl;
    	}
    	Shared_ptr<T>& operator=(const Shared_ptr &other) {
    		if(this == &other)	return *this;
    		(*other.use_count)++;
    		if(ptr && --(*use_count) == 0) {
    			delete ptr;
    			delete use_count;
    		}
    		ptr = other.ptr;
    		use_count = other.use_count;
    		return *this;
    	}
    	T& operator*() {	// 返回指针的引用
    		return *ptr;
    	}
    	T* operator->() {
    		return ptr;
    	}
    	int get_use_count() {
    		if(use_count == nullptr)	return 0;
    		return *use_count;
    	}
    	~Shared_ptr() {
    		if(ptr && --(*use_count) == 0) {
    			delete ptr;
    			delete use_count;
    			cout << "Destroy" << endl;
    		}
    	}
    };
    
  • 相关阅读:
    Centos7.3安装jdk和maven
    Centos7.3安装sftp服务和ssh
    Centos7.3安装,并设置网络和防火墙
    Docker 常用命令
    Tomcat配置
    MySQL配置
    配置JDK
    JTable的模型
    JavaBean和List<JavaBean>
    JavaBean
  • 原文地址:https://www.cnblogs.com/Jiaaaaaaaqi/p/12327488.html
Copyright © 2011-2022 走看看