C++自定义智能指针

cpp 复制代码
template <class T>
class counted_ptr;

// 智能指针引用计数类
template <class T>
class Ref_Ptr
{
	friend class counted_ptr<T>;
private:
	T* m_pTtr; // 实际的指针
	size_t counted_ptr; // 引用计数
	Ref_Ptr(T* p);
	virtual ~Ref_Ptr();
};

template <class T>
Ref_Ptr<T>::Ref_Ptr(T* p)
{
	m_pTtr = p;
	counted_ptr = 1;
	cout<<"Ref_Ptr() 构造函数调用!"<<endl;
}

template <class T>
Ref_Ptr<T>::~Ref_Ptr()
{
	if (m_pTtr)
	{
		cout<<"~Ref_Ptr() 析构函数函数调用"<<endl;
		delete m_pTtr;
		counted_ptr = 0;
	}
	m_pTtr = NULL;
}

// 智能指针对象
template <class T>
class counted_ptr
{
private:
	Ref_Ptr<T>* m_pRef; // 引用计数
public:
	counted_ptr();
	counted_ptr(T* p);
	~counted_ptr();
	// 重载运算=,将左对象引用计数-1,并判断是否delete;将右对象+1;
	counted_ptr<T> & operator = (counted_ptr& other);
	// 重载指针操作*,->
	T& operator *();
	T* operator ->();
	// 拷贝构造函数,引用计数+1
	counted_ptr(counted_ptr<T>& other);
};
template <class T>
counted_ptr<T>::counted_ptr()
{
	m_pRef = NULL;
}

template<class T>
counted_ptr<T>::counted_ptr(T* p)
{
	m_pRef = new Ref_Ptr<T>(p);
	cout<<"counted_ptr(T* p) 构造函数调用"<<endl;
}

template <class T>
counted_ptr<T>::counted_ptr(counted_ptr<T>& other)
{
	this->m_pRef = other.m_pRef;
	++(m_pRef->counted_ptr);
	cout<<"counted_ptr(& other) 拷贝构造函数被调用,当前引用计数"<< this->m_pRef->counted_ptr<<endl;
}

template <class T>
counted_ptr<T>& counted_ptr<T>::operator=(counted_ptr& other)
{
	// 将右操作对象引用计数+1
	++(other.m_pRef->counted_ptr);

	// 由于左操作对象指向了新对象,需要将操作数-1;
	// 同时也防止了自赋值的方式.
	// 首先要判断这个对象是否已经指向了其他对象,这个很重要!防止左指针对象为null的情况.
	if (this->m_pRef)
	{
		if (--(this->m_pRef->counted_ptr) == 0)
		{
			delete this->m_pRef;
		}
	}

	this->m_pRef = other.m_pRef;
	cout<<"operator = 被调用,当前引用计数"<< this->m_pRef->counted_ptr<<endl;
	return *this;
}

template <class T>
T& counted_ptr<T>::operator *()
{
	return *(m_pRef->m_pTtr);
}

template <class T>
T* counted_ptr<T>::operator->()
{
	return (m_pRef->m_pTtr);
}

template <class T>
counted_ptr<T>::~counted_ptr()
{cout<<"~counted_ptr() 析构函数被调用"<<endl;
	if ((--m_pRef->counted_ptr) == 0)
	{
		cout<<"删除"<<endl;
		delete m_pRef;
		m_pRef = NULL;
	}
	if (m_pRef)
	{
		cout<<"当前引用计数:"<< m_pRef->counted_ptr<<endl;
	}
}


int  main()
{
	counted_ptr<int>* pPtr = NULL;

	{
		counted_ptr<int> g_ptr;
		{
			// 声明一个ptr1智能指针,并测试*运算符
			counted_ptr<int> ptr1(new int(4));
			counted_ptr<int> ptr2;
			cout<< "*ptr1="<< *ptr1<<endl;
			// 将生存期小的ptr1赋值给生存期更大的g_ptr;
			ptr2 = ptr1;
			system("pause");
			g_ptr = ptr1;
			system("pause");
		}
		// new int(4)并没有销毁,因为引用计数还有1个
		cout<<" *g_ptr="<<*g_ptr << endl;
		system("pause");
	}
	system("pause");
	getchar();
	return 0;
}

参考

std::shared_ptr - cppreference.com


创作不易,小小的支持一下吧!

相关推荐
lkbhua莱克瓦2419 分钟前
集合进阶8——Stream流
java·开发语言·笔记·github·stream流·学习方法·集合
骑自行车的码农24 分钟前
🍂 React DOM树的构建原理和算法
javascript·算法·react.js
20岁30年经验的码农38 分钟前
Java Elasticsearch 实战指南
java·开发语言·elasticsearch
雾岛听蓝42 分钟前
C++ 类和对象(一):从概念到实践,吃透类的核心基础
开发语言·c++·经验分享·笔记
CoderYanger1 小时前
优选算法-优先级队列(堆):75.数据流中的第K大元素
java·开发语言·算法·leetcode·职场和发展·1024程序员节
希望有朝一日能如愿以偿1 小时前
力扣每日一题:能被k整除的最小整数
数据结构·算法·leetcode
Controller-Inversion1 小时前
力扣53最大字数组和
算法·leetcode·职场和发展
rit84324991 小时前
基于感知节点误差的TDOA定位算法
算法
m0_372257021 小时前
ID3 算法为什么可以用来优化决策树
算法·决策树·机器学习
TracyCoder1231 小时前
MySQL 实战宝典(八):Java后端MySQL分库分表工具解析与选型秘籍
java·开发语言·mysql