没有经过严格测试。
代码如下
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;
}