shared_ptr

好多细节要注意!!!

(此代码存在线程安全问题,下个博客讲)

#include<iostream>
#include<stdlib.h>
//C++11
//shared_ptr:功能全,支持拷贝,引用计数。设计复杂,循环引用
namespace juju
{
	template<class T>
	class shared_ptr
	{
	public:
		shared_ptr(T* ptr)
			:_ptr(ptr)
			,_pcount(new int(1))//将它初始化为1
		{}

		~shared_ptr()
		{
			if (--(*_pcount) == 0)
			{
				std::cout << "delete:" << _ptr << std::endl;
				delete _ptr;
				delete _pcount;
			}
		}

		shared_ptr(const shared_ptr<T>& ap)//拷贝构造
			:_ptr(ap._ptr)
			, _pcount(ap._pcount)
		{
			++(*_pcount);
		}

		shared_ptr<T>& operator=(shared_ptr<T> & ap)
		{
			//防止自己给自己赋值
			//if (this != &ap)
			if (_ptr != ap._ptr)//优化同一块空间的赋值
			{
				if (--(*_pcount))//当这块空间只剩最后一个的时候,再去释放他。不能直接释放
				{
					delete _ptr;//不能直接释放,如果第一块空间既有ap1,也有ap2,那你直接释放掉空间,ap2的内存就出问题了
					delete _pcount;
				}
				_ptr = ap._ptr;
				_pcount = ap._pcount;

				++(*_pcount);
			}
			return *this;
		}

		T& operator*()
		{
			return *_ptr;
		}

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

	private:
		T* _ptr;
		int* _pcount;//不能将它弄成静态,万一在两块空间,其中一块会没被释放
	};
}void test_shared_ptr()
{
	juju::shared_ptr<int> ap1(new int);
	juju::shared_ptr<int> ap2(ap1);
	ap1 = ap2;//如果同一块空间里的相互赋值,自娱自乐,我们要在赋值那进行优化

	juju::shared_ptr<int> ap3(new int);
	ap1 = ap3;
}

int main()
{
	test_shared_ptr();
	system("pause");
	return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值