模拟实现C++/boost库智能指针auto_ptr scoped_ptr和share_ptr

#pragma once
 
#include<iostream>
using namespace std;
//1,AutoPtr 管理权转让(不建议使用)
//2,scopedptr 防拷贝 声明为protected private 只声明不实现(建议使用)
//3,shareptr 引用计数 增减计数,最后一个时释放对象
//4,xxxarrar  管理数组。只需要重载[]
//5,weakptr  弱指针 辅助shareptr 解决循环引用的问题,不增加赋值后的引用计数打破shareptr的循环引用不能释放


//智能指针:自动化管理指针所指向的动态资源的释放
template<class T>
class AutoPtr
{
public:
AutoPtr(T* ptr)//构造函数
:_ptr(ptr)
{}


AutoPtr(AutoPtr<T>& ap)//拷贝构造
:_ptr(ap._ptr)
{
ap._ptr = NULL;
}


AutoPtr<T>& operator=( AutoPtr<T>& ap)//赋值运算符重载
{
if (this != &ap)//判断自赋值
{
        delete _ptr;
_ptr = ap._ptr;
ap._ptr = NULL;
}

return *this;
}
~AutoPtr()//析构函数
{
if (_ptr)
{
cout << "delete:" << _ptr << endl;
delete _ptr;
_ptr = NULL;//防止野指针
}
}


T& operator*()//*的重载
{
return *_ptr;
}
T* operator->()//->的重载
{
return _ptr;
}
protected:
T* _ptr;
};




struct A
{
int _a;
};


void TestAutoPtr()
{
AutoPtr<int> ap1(new int(1));
AutoPtr<int> ap2(ap1);
AutoPtr<int>ap3(new int(2));
ap3 = ap2;
*ap3 = 10;
AutoPtr<A>ap4(new A);
ap4->_a = 10;
}


template<class T>
class ScopedPtr
{
public:
ScopedPtr(T* ptr)//构造函数
:_ptr(ptr)
{}


~ScopedPtr()//析构函数
{
if (_ptr)
{
cout << "delete:" << _ptr << endl;
delete _ptr;
}
}


T& operator*()
{
return *ptr;
}
T* operator->()
{
return _ptr;
}


protected:
ScopedPtr(const ScopedPtr<T>& sp);//拷贝构造
ScopedPtr<T>& operator = (ScopedPtr<T>&sp);//赋值运算符(防拷贝)
//只声明不实现 放入protected或者private中
protected:
T* _ptr;
};


void TestScopedPtr()
{
ScopedPtr<int> sp1(new int(1));
//ScopedPtr<int> sp2(sp1);
}


template<class T>
class SharedPtr
{
public:
SharedPtr(T* ptr)//构造函数
:_ptr(ptr)
, _pcount(new long(1))//引用计数
{}


SharedPtr(const SharedPtr<T>& sp)//拷贝构造
:_ptr(sp._ptr)
, _pcount(sp._pcount)
{
++(*_pcount);//引用计数+1
}


//SharedPtr<T>& operator=(const SharedPtr<T>& sp)//赋值运算符的重载 (传统赋值方法)
//{
// if (_ptr != sp._ptr)//判断自赋值
// {
// if (--(*_pcount) == 0)
// {
// delete _ptr;
// delete _pcount;
// }
// _ptr = sp._ptr;
// _pcount = sp._pcount;
// ++(*_pcount);
// }
// return *this;
//}
SharedPtr<T>& operator=(SharedPtr<T> sp)//现代的赋值方法  
{
swap(_ptr, sp._ptr);
swap(_pcount, sp._pcount);
return *this;
}


~SharedPtr()
{
if (--(*_pcount)==0)
{
delete _ptr;
delete _pcount;
}
}
T& operator*()
{
return *_ptr;
}
T* operator->()
{
return _ptr;
}


long UseCount()
{
return *_pcount;
}


T* GetPtr()
{
return _ptr;
}
protected:
T* _ptr;
long* _pcount;//引用计数
};


void TestSharePtr()
{
SharedPtr<int> sp1(new int(1));
cout << "sp1:" << sp1.UseCount() << endl << endl;


SharedPtr<int> sp2(sp1);
cout << "sp1:" << sp1.UseCount() << endl;
cout << "sp2:" << sp2.UseCount() << endl << endl;
SharedPtr<int> sp3(sp2);
cout << "sp2:" << sp2.UseCount() << endl;
cout << "sp3:" << sp3.UseCount() << endl << endl;


cout << "sp1:" << sp1.UseCount() << endl;
cout << "sp2:" << sp2.UseCount() << endl;
cout << "sp3:" << sp3.UseCount() << endl << endl;


sp1 = sp1;
cout << "sp1:" << sp1.UseCount() << endl << endl;
sp1 = sp2;
cout << "sp1:" << sp1.UseCount() << endl;
cout << "sp2:" << sp2.UseCount() << endl << endl;


cout << "sp1:" << sp1.UseCount() << endl;
cout << "sp2:" << sp2.UseCount() << endl;
cout << "sp3:" << sp3.UseCount() << endl << endl;


SharedPtr<int> sp4(new int(2));
cout << "sp4:" << sp4.UseCount() << endl << endl;


sp1 = sp4;
cout << "sp1:" << sp1.UseCount() << endl;
cout << "sp4:" << sp4.UseCount() << endl << endl;


cout << "sp1:" << sp1.UseCount() << endl;
cout << "sp2:" << sp2.UseCount() << endl;
cout << "sp3:" << sp3.UseCount() << endl;
cout << "sp4:" << sp4.UseCount() << endl;

}


#define _CRT_SECURE_NO_WARNINGS 1


#include"smartptr.h"


using namespace std;


//void Test1()
//{
// int* p = new int(1);
// AutoPtr<int> ap(p);
// if (1)
// {
// return ;
// }
//}
//
//void DoSomething()
//{
// if (1)
// {
// throw 1;
// }
//}
//
//void Test2()
//{
// int*p = new int(1);
// AutoPtr<int> ap(p);
//
// DoSomething();
// try
// {
// DoSomething();
// }
// catch (...)
// {
// delete p;
// throw;
// }
// delete p;
//}
//int main()
//{
// try
// {
// Test1();
// Test2();
// }
// catch (...)
// {
// cout << "未知异常" << endl;
// }
// system("pause");
// return 0;
//}




int main()
{
//TestAutoPtr();
//TestScopedPtr();
TestSharePtr();
system("pause");
return 0;
}


引用计数运行结果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值