《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