简单实现auto_ptr,shared_ptr,weak_ptr

没有经过严格测试。

代码如下

Auto_ptr.h

#include<assert.h>
using namespace std;
template<typename Type>class Auto_ptr{
private:
	Type* pointer;
public:
	Auto_ptr(Type* t=NULL):pointer(t){}
	Auto_ptr(Auto_ptr<Type>& other){
		pointer=other.pointer;
		other.pointer=NULL;
	}
	~Auto_ptr(){
		delete pointer;
	}
	Auto_ptr& operator=(Auto_ptr& other){
		if(this!=&other){
			delete pointer;
			pointer=other.pointer;
			other.pointer=NULL;
		}
		return *this;
	}
	Type& operator*(){
		assert(pointer!=NULL);
		return *pointer;
	}
	Type* get(){
		return pointer;
	}
	Type* operator->(){
		assert(pointer!=NULL);
		return pointer;
	}
};

Shared_ptr.h 包括Shared_ptr和Weak_ptr

#include<assert.h>
template<typename Type>class Cnt{
public:
	int shared_cnt;
	int weak_cnt;
	Type* t_ptr;
	Cnt(){
		++cntInit;
		cout<<"Cnt cntInit="<<cntInit<<endl;
	}
	~Cnt(){
		++cntDes;
		cout<<"~Cnt cntDes="<<cntDes<<endl;
	}
	static int cntInit;
	static int cntDes;
};
template<typename Type> int Cnt<Type>::cntInit=0;
template<typename Type> int Cnt<Type>::cntDes=0;

template<typename Type>class Weak_ptr;

template<typename Type>class Shared_ptr{
private:
	Cnt<Type>* cnt;
	void minusSharedCnt(){
		if(--(cnt->shared_cnt)==0){
			Type* p=cnt->t_ptr;
			cnt->t_ptr=NULL;
			if(cnt->weak_cnt==0){
				delete p;
				delete cnt;
			}
			else delete p;
		}
	}
public:
	Shared_ptr(Type* p=NULL){
		cnt=new Cnt<Type>();
		cnt->shared_cnt=1;
		cnt->weak_cnt=0;
		cnt->t_ptr=p;
	}
	~Shared_ptr(){
		minusSharedCnt();
	}
	Shared_ptr(const Shared_ptr<Type>& other){
		cnt=other.cnt;
		cnt->shared_cnt+=1;
	}
	Shared_ptr(const Weak_ptr<Type>& other);
	Shared_ptr<Type>& operator=(const Weak_ptr<Type>& other);
	Shared_ptr<Type>& operator=(const Shared_ptr<Type>& other){
		if(cnt->t_ptr!=other.cnt->t_ptr){
			minusSharedCnt();
			cnt=other.cnt;
			cnt->shared_cnt+=1;
		}
		return *this;
	}
	Type& operator*(){
		assert(cnt->t_ptr!=NULL);
		return *cnt->t_ptr;
	}
	Type* operator->(){
		assert(cnt->t_ptr!=NULL);
		return cnt->t_ptr;
	}
	Type* get(){
		return cnt->t_ptr;
	}
	template<typename Type> friend class Weak_ptr;
};

template<typename Type>class Weak_ptr{
private:
	Cnt<Type>* cnt;
	void minusWeakCnt(){
		if((--(cnt->weak_cnt))==0){
			if(cnt->shared_cnt==0){
				delete cnt;
			}
		}
	}
public:
	Weak_ptr(){
		cnt=new Cnt<Type>();
		cnt->shared_cnt=0;
		cnt->weak_cnt=1;
		cnt->t_ptr=NULL;
	}
	Weak_ptr(const Shared_ptr<Type>& other){
		cnt=other.cnt;
		cnt->weak_cnt+=1;
	}
	Weak_ptr(const Weak_ptr<Type>& other){
		cnt=other.cnt;
		cnt->weak_cnt+=1;
	}
	Weak_ptr& operator=(const Shared_ptr<Type>& other){
		if(cnt->t_ptr!=other.cnt->t_ptr){
			minusWeakCnt();
			cnt=other.cnt;
			cnt->weak_cnt+=1;
		}
		return *this;
	}
	Weak_ptr& operator=(const Weak_ptr<Type>& other){
		if(cnt->t_ptr!=other.cnt->t_ptr){
			minusWeakCnt();
			cnt=other.cnt;
			cnt->weak_cnt+=1;
		}
		return *this;
	}
	~Weak_ptr(){
		minusWeakCnt();
	}
	Shared_ptr<Type> lock(){
		Shared_ptr<Type> s(*this);
		return s;
	}
	template<typename typename>friend class Shared_ptr;
};

template<typename Type>Shared_ptr<Type>::Shared_ptr(const Weak_ptr<Type>& other){
	cnt=other.cnt;
	cnt->shared_cnt+=1;
}
template<typename Type>Shared_ptr<Type>& Shared_ptr<Type>::operator=(const Weak_ptr<Type>& other){
	if(cnt->t_ptr!=other.cnt->t_ptr){
		minusSharedCnt();
		cnt=other.cnt;
		cnt->shared-cnt+=1;
	}
}

main.cpp测试文件

#include<string>
#include<iostream>
#include"Auto_ptr.h"
#include"Shared_ptr.h"
using namespace std;

class C2;

class C1{
private:
	Shared_ptr<C2> s;
public:
	C1(){
		cout<<"C1"<<endl;
	}
	void setC2(const Shared_ptr<C2>& s){
		this->s=s;
	}
	~C1(){
		cout<<"~C1"<<endl;
	}
};
class C2{
private:
	Weak_ptr<C1> s;
public:
	C2(){
		cout<<"C2"<<endl;
	}
	void setC1(const Shared_ptr<C1>& s){
		this->s=s;
	}
	~C2(){
		cout<<"~C2"<<endl;
	}
};

int main(){
	
	Weak_ptr<C2> c3;
	Shared_ptr<C1> c5;
	{
		Shared_ptr<C1> c1(new C1());
		Shared_ptr<C2> c2(new C2());
		c1->setC2(c2);
		c2->setC1(c1);
		c3=c2;
		c5=c1;
	}
	cout<<"Split--------------"<<endl;
	Shared_ptr<C2> c4=c3.lock();
	if(c4.get()==NULL)
		cout<<"NULL"<<endl;
		
	return 0;
}



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值