C++ Primer 第5版——shared_ptr和StrBlob类

博客围绕《C++ Primer》第五版第12章,介绍动态内存与智能指针。C++ 11提供shared_ptr和unique_ptr管理动态内存,shared_ptr允许多指针指向同一对象,有引用计数机制。书中定义StrBlob类引用shared_ptr实现数据共享,还给出了增加成员函数后的StrBlob类代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

《C++ Primer》第五版,第12章,主要是介绍了动态内存与智能指针方面的知识点。

C++ 11新标准,提供了两种智能指针类型shared_ptr和unique_ptr来管理动态内存。智能指针的行为类似常规指针,重要的区别是它负责自动释放所指向的对象。

我们平时创建动态内存及释放内存的方式,例如:

int *pt = new int(11);
delete pt;

使用智能指针的方式是:

shared_ptr<int> p = make_shared<int>(11);

shared_ptr允许多个指针指向同一个对象,即指向同一块内存。每个shared_ptr都有一个关联的计数器,通常称为引用计数无论何时我们拷贝一个shared_ptr,计数器都会递增。例如,当用一个shared_ptr初始化另一个shared_ptr,或将它作为参数传递给一个函数以及作为函数返回值时,它所关联的计数器都会递增。当一个shared_ptr被销毁(比如离开所属的作用域),其计数器就会减1。一旦一个shared_ptr的计数器变为0,它就会自动释放自己所管理的对象

智能指针shared_ptr和unique_ptr所支持的操作见下图:

使用动态内存的一个常见原因时允许多个对象共享相同的状态。书中定义了StrBlob类,引用了shared_ptr的功能,实现我们所希望的数据共享。我们在StrBlob设置一个shared_ptr来管理动态分配的vector。此shared_ptr的成员将记录有多少个StrBlob共享相同的vector,并在vector的最后一个使用者被销毁时释放vector。

StrBlob类代码如下,但比书中多增加了两个成员函数,printBlob()用于输出vector容器的内容,useCount()用于统计共享内存的引用计数。

// StrBlob.h

#pragma once
#include <string>
#include <vector>
#include <iostream>
#include <iomanip>
#include <initializer_list>
#include <algorithm>
#include <memory>
using namespace std;

class StrBlob
{
public:
	typedef vector<string>::size_type size_type;

	StrBlob();
	~StrBlob();
	StrBlob(initializer_list<string> il);

	size_type size() const { return data->size(); }
	bool empty() const { return data->empty(); }

	// 添加和删除元素
	void push_back(const string &t) { data->push_back(t); }
	void pop_back();

	// 元素访问
	string &front();
	string &front() const;
	string &back();
	string &back() const;

	// 打印元素
	void printBlob();
	// 统计共享动态内存引用计数
	unsigned long useCount();

private:
	shared_ptr<vector<string>> data;
	void check(size_type i, const string &msg) const;

};
// StrBlob.cpp

#include "StrBlob.h"

StrBlob::StrBlob() : data(make_shared<vector<string>>())
{
}

StrBlob::StrBlob(initializer_list<string> il) : data(make_shared<vector<string>>(il))
{
}

StrBlob::~StrBlob()
{
}

void StrBlob::check(size_type i, const string &msg) const
{
	if (i >= data->size())
		throw out_of_range(msg);
}

string& StrBlob::front()
{
	check(0, "front on empty StrBlob");
	return data->front();
}

string& StrBlob::front() const
{
	check(0, "front on empty StrBlob");
	return data->front();
}

string& StrBlob::back()
{
	check(0, "back on empty StrBlob");
	return data->front();
}

string& StrBlob::back() const
{
	check(0, "back on empty StrBlob");
	return data->front();
}

void StrBlob::pop_back()
{
	check(0, "pop_back on empty StrBlob");
	data->pop_back();
}

void StrBlob::printBlob()
{
	for_each(data->begin(), data->end(), [](const string& s) {cout << left << setw(12) << s;});
	cout << endl;
}

// 统计共享动态内存的引用计数
unsigned long StrBlob::useCount()
{
	return data.use_count();
}

mian()函数中的调用部分如下:

#include "StrBlob.h"

int main()
{
	StrBlob blob1;
	cout << blob1.size() << endl;
	cout << blob1.useCount() << endl;

	{
		StrBlob blob2({"Hello", "Morning", "Best Wishes"});
		blob1 = blob2;
		blob2.push_back("Good Luck");
		cout << blob1.size() << endl;
		cout << blob1.useCount() << endl;
		cout << blob2.useCount() << endl;
	}
	blob1.printBlob();
	cout << blob1.useCount() << endl;
	
	StrBlob blob3({"apple", "pear", "banana", "orange"});
	blob3.printBlob();
	cout << blob3.useCount() << endl;
	return 0;
}

其允许结果如下:

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值