《C++ Primer》学习笔记 — 拷贝控制

一、拷贝控制

1、三五法则

(1)需要析构函数的类也需要拷贝和赋值操作 — 例如对象需要在析构函数中释放动态内存,则需要在拷贝构造函数中考虑为新对象分配新内存,而不能进行位拷贝。
(2)需要拷贝操作的类也需要赋值操作,反之亦然

2、析构函数不能是删除的成员

如果析构函数被删除,将无法销毁此类型的对象。因此,我们无法在栈上创建该对象。对于在堆上创建的对象,我们无法对其进行销毁:

class CLS_Test
{
   
public:
	~CLS_Test() = delete;
};

int main()
{
   
	CLS_Test test; // invalid
	CLS_Test* test = new CLS_Test;
	delete test; // invalid
}

3、引用计数

我们尝试实现一个引用计数法保存的模板类,使其在成员数据发生改变时重置引用计数:

#include <iostream>
#include <functional>
using namespace std;

template<class T>
class CLS_RefCount
{
   
public:
	CLS_RefCount(const T* _ptr)
	{
   
		init(_ptr);
	}

	CLS_RefCount(const CLS_RefCount& other)
	{
   
		copy(other);
	}

	CLS_RefCount operator=(const CLS_RefCount& other)
	{
   
		(*m_iRefCount)++;
		release();
		m_ptr = other.m_ptr;
		m_iRefCount = other.m_iRefCount;
	}
	
	~CLS_RefCount() 
	{
   
		release();
	}

	void changeVal(function<T(T)> func)
	{
   
		release();
		m_ptr = other.m_ptr;
		m_iRefCount = other.m_iRefCount;
		(*m_iRefCount)++;
		return *this;
	}

	T operator*()
	{
   
		return *m_ptr;
	}

	int refCount()
	{
   
		return *m_iRefCount;
	}

private:
	T* m_ptr;
	size_t *m_iRefCount;
	
	void init(const T* _ptr)
	{
   
		m_ptr = const_cast<T*>(_ptr);
		m_iRefCount = new size_t(1);
	}

	void copy(const CLS_RefCount& other)
	{
   
		m_ptr = other.m_ptr;
		m_iRefCount = other.m_iRefCount;
		(*m_iRefCount)++;
	}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值