shellmad-16_C++新特性 写时拷贝

上节课引用技术遗留了两个问题

  1. 当我们资源进行修改, 所有引用该对象的资源都会被修改.

解决该问题的思路: 写时拷贝(Copy On Write) : 在写的时候, 对资源进行重新拷贝一份, 当某个资源要进行修改的时候, 我们C++检测到这种修改, 然后会对这个资源进行一次拷贝, 接下来你的修改与原资源没有关系了, 因为你改变的是拷贝的值, 所以如果这么做, 能够解决上述问题
但是如果这么做, 要对引用技术进行相应的加减, 新的资源, 要有新的引用计数器, 要新产生,并且变为1; 原始的资源的引用计数要-1, 所以要涉及到两个事情
1.1. 资源的拷贝
1.2. 引用计数器的加减
分析代码:

// Student.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream.h>
#include <cstring>

struct RefValue 
{

	RefValue(const char* pszName);
	~RefValue();

	void AddRef();
	void Release();

	char* m_pszName;
	int   m_nCount;
};

RefValue::RefValue(const char* pszName)
{
	m_pszName = new char[256];
	
	strcpy(m_pszName, pszName);
	m_nCount = 1;

}

RefValue::~RefValue()
{
	if (m_pszName != NULL)
	{
		delete m_pszName;
		m_pszName = NULL;
	}
}

void RefValue::AddRef()
{
	m_nCount++;
}

void RefValue::Release()
{
	if (--m_nCount == 0)
	{
		delete this;
	}
	
}


class CStudent
{
public:
	CStudent(const char* pszName);
	CStudent(CStudent& obj);

	void SetName(const char* pszName);

	~CStudent();

	CStudent& operator=(CStudent& obj);

	void release();

	void Show()
	{
		if (m_pValue->m_nCount > 0)
		{
			cout << hex << (int&)m_pValue->m_pszName << m_pValue->m_pszName <<endl;
		}
	}

private:
	RefValue* m_pValue;
};

void CStudent::SetName(const char* pszName)
{
	m_pValue->Release();
	m_pValue = new RefValue(pszName);
}

CStudent::CStudent(const char* pszName)
{
	m_pValue = new RefValue(pszName);
}

CStudent::CStudent(CStudent& obj)
{
	m_pValue = obj.m_pValue;
	m_pValue->AddRef();
}

CStudent::~CStudent()
{
	release();
}


CStudent& CStudent::operator=(CStudent& obj)
{
	if (obj.m_pValue == m_pValue)
	{
		return *this;
	}

	m_pValue->Release();

	m_pValue = obj.m_pValue;
	m_pValue->AddRef();

	return *this;
}

void CStudent::release()
{
	m_pValue->Release();
}

int main(int argc, char* argv[])
{
	CStudent stu1("zhang san");

	CStudent stu2("li si");

	CStudent stu3 = stu2;


	stu2.Show();
	stu3.Show();

	stu2.SetName("li si2");

	stu2.Show();
	stu3.Show();

	return 0;
}

还是一样的道理, 创建一个计数器对象struct RefValue
然后资源和计数器绑定

Struct RefValue{
	...
	char* m_pszName;
	int   m_nCount;
}

计数器是普通的变量int, 因为RefValue是指针

接着
计数器创建对象, 计数器赋值为1


RefValue::RefValue(const char* pszName)
{
	m_pszName = new char[256];
	
	strcpy(m_pszName, pszName);
	m_nCount = 1;

}

释放的时候释放掉

RefValue::~RefValue()
{
	if (m_pszName != NULL)
	{
		delete m_pszName;
		m_pszName = NULL;
	}
}

该+1的+1
该释放的释放

void RefValue::AddRef()
{
	m_nCount++;
}
void RefValue::Release()
{
	if (--m_nCount == 0)
	{
		delete this;
	}
	
}

与上一次代码唯一改变的地方 =运算符的重载CStudent& operator=(CStudent& obj); 以及拷贝构造CStudent(CStudent& obj);

当进行拷贝构造的时候, 用的是浅拷贝, 并且资源计数器+1

CStudent::CStudent(CStudent& obj)
{
	m_pValue = obj.m_pValue;
	m_pValue->AddRef();
}

当进行=运算符重载的时候, 依然是浅拷贝, 资源计数器 + 1

CStudent& CStudent::operator=(CStudent& obj)
{
	if (obj.m_pValue == m_pValue)
	{
		return *this;
	}

	m_pValue->Release();

	m_pValue = obj.m_pValue;
	m_pValue->AddRef();

	return *this;
}

但是在修改资源的时候void SetName(const char* pszName);
会对原来的计数器进行释放m_pValue->Release();

  • 如果原来的计数器减少为了0, 那么就会释放
 void RefValue::Release()
{
	if (--m_nCount == 0)
	{
		delete this;
	}
	
}
  • 如果没有减为0, 那么就会减少计数器的引用

然后就会创建新的计数器, 赋给当前的对象m_pValue = new RefValue(pszName);
核心代码

void CStudent::SetName(const char* pszName)
{
	m_pValue->Release();
	m_pValue = new RefValue(pszName);
} 

运行代码:
此时stu2stu3同时在使用“li si”这个资源, 所以引用计数为2次, 两者的m_pszName是一模一样的
在这里插入图片描述
如果调用setName, 会对其中一个资源(m_pszName)进行修改
在这里插入图片描述
要修改资源了, 对原有资源的引用计数器进行释放操作
在这里插入图片描述
进行原有资源释放操作, 需要判断原有资源引用计数
在这里插入图片描述
新创建一个对象(代码中 "li si2") 的引用计数器赋给当前引用计数器
在这里插入图片描述
计数器变为1
在这里插入图片描述
最后的结果
在这里插入图片描述
本来两者拥有的是同一种资源, 但是其中某一个资源得到了修改, 就会重新申请一份资源, 对新申请的资源进行修改, 就是写时拷贝操作

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值