C++没有类似Java、C#等语言的垃圾回收机制,内存管理是最为头痛的工作。

new、delete以及指针的不恰当运用是C++中造成资源获取/释放问题的根源。

智能指针是解决这些问题的一种方案,boost.smart_ptr库提供了六种智能指针,包括:

scoped_ptrscoped_arrayshared_ptrshared_arrayweak_ptrintrusive_ptr

它们都是轻量级对象,速度与原始指针相差无几,都是异常安全的。

要使用smart_ptr组件,需要包含头文件 #include <boost/smart_ptr.hpp>

shared_ptr是一个最像指针的“智能指针”,是boost.smart_ptr库中最有价值、最重要的组成部分。已收入了C++11标准。

shared_ptr是引用计数的智能指针,可以被自由的拷贝和赋值,在任意地方共享它,当没有代码使用(引用计数为0)时删除被包装的动态分配的对象。

shared_ptr可以安全的放到标准容器中,是在STL容器中存储指针的最标准解法。

应用举例:

#include "stdafx.h"
#include <iostream>
#include <cassert>
#include <string>
#include <vector>
#include <boost/smart_ptr.hpp> using std::cout;
using std::endl;
using std::string;
using std::vector; int _tmain(int argc, _TCHAR* argv[])
{
boost::scoped_ptr<string> pstr(new string("scoped_ptr test"));
assert(pstr);
assert(pstr != nullptr);
*pstr = "hello test";
cout << *pstr << ": " << pstr->size(); boost::scoped_ptr<string> another_p_str;
assert(!another_p_str); if (NULL == another_p_str)
{
cout << "\nNull pointer!\n";
} boost::shared_ptr<string> sps(boost::make_shared<string>("shared_ptr_test"));
cout << "\n" << sps->c_str() << endl;
boost::shared_ptr<string> sps2(sps);
*sps2 = "shared_ptr is smart!";
cout << *sps << endl; assert(sps == sps2); //shared_ptr用于容器
typedef vector<boost::shared_ptr<int>> VSI;
VSI v;
v.push_back(boost::make_shared<int>(1));
v.push_back(boost::make_shared<int>(2));
v.push_back(boost::make_shared<int>(3));
v.push_back(boost::make_shared<int>(4));
v.push_back(boost::make_shared<int>(5)); cout << "\nVevtor: ";
boost::shared_ptr<int> pv = v[0];
*pv = 1000;
for (size_t i = 0; i < v.size(); ++i)
{
cout << *v[i] << " ";
}
return 0;
}

在开发实践中,个人觉的最应当使用智能指针的两种场合:

(1) 应用于标准容器

在使用容器管理大量对象指针的时候,必须编写额外的大量代码来保证指针最终被正确释放,通常很麻烦而且容易出错。将shared_ptr作为容器的元素,如vector<shared_ptr<T> >,与存储原始指针的容器所能实现的功能几乎一样,而不用担心资源泄漏。

(2) 应用于工厂模式

在程序中编写自己的工厂类或者工厂函数时,通常需要在堆上使用new动态分配一个对象然后返回对象的指针。这种做法很不安全,应为很容易忘记对指针调用delete。

使用shared_ptr可以解决这个问题,只需要修改工厂方法的接口,不再返回一个原始指针,而是返回一个被shared_ptr包装的智能指针,这样可以很好地保护系统资源,而且会更好地控制对接口的使用。

使用代码来解释shared_ptr应用于工厂模式的用法:

#ifndef OPERATION_H_
#define OPERATION_H_
#include <boost/smart_ptr.hpp> enum OperationType
{
ADD,
SUB,
MUL,
DIV
}; //运算基类
class Operation
{
public:
virtual double GetResult() const = 0; Operation()
: m_Operand1(0.0)
, m_Operand2(0.0)
{ } void SetOperrand1(const double operand1)
{
m_Operand1 = operand1;
}
double GetOperand1() const
{
return m_Operand1;
} void SetOperrand2(const double operand2)
{
m_Operand2 = operand2;
}
double GetOperand2() const
{
return m_Operand2;
} protected:
virtual ~Operation(){} protected:
double m_Operand1;
double m_Operand2;
};
//加法
class Add : public Operation
{
public:
double GetResult() const override;
};
//减法
class Sub : public Operation
{
public:
double GetResult() const override;
};
//乘法
class Mul : public Operation
{
public:
double GetResult() const override;
};
//除法
class Div : public Operation
{
public:
double GetResult() const override;
}; //工厂类
class OperationFactory
{
public:
static boost::shared_ptr<Operation> CreateOperation(OperationType type)
{
boost::shared_ptr<Operation> oper;
switch (type)
{
case ADD:
oper = boost::make_shared<Add>();
break;
case SUB:
oper = boost::make_shared<Sub>();
break;
case MUL:
oper = boost::make_shared<Mul>();
break;
case DIV:
oper = boost::make_shared<Div>();
break;
default: ;
}
return oper;
}
}; #endif

调用代码:

//通过工厂方法创建对象
auto pOper = OperationFactory::CreateOperation(MUL);
if (nullptr != p)
{
pOper->SetOperrand1(100); //可以像普通指针一样使用
pOper->SetOperrand2(123);//不必担心内存泄漏,shared_ptr会自动管理指针
cout << "\n" << pOper->GetResult();
}

C++11标准中std::shared_ptr,功能与boost::shared_ptr基本相同,完全可以定价互换。

包含头文件#incude <memory>

boost--smart_ptr库的更多相关文章

  1. 初探boost之smart_ptr库学习笔记

    概述 Boost.smart_ptr库提供了六种智能指针,除了shared_ptr 和 weak_ptr 以外还包含 scoped_ptr .scoped_array . shared_array . ...

  2. [boost] : test库

    最小化的测试套件minimal_test test库提供一个最小化的测试套件minimal_test, 类似lightweight_test适合入门级测试. 需要包含文件文#include <b ...

  3. [boost] : lightweight_test库

    lightweight_test轻量级单元测试框架, 只支持最基本的单元测试, 不支持测试用例, 测试套件的概念, 简单小巧, 适合要求不高或者快速测试的工作. 基本用法 需要包含头文件#includ ...

  4. 如何在WINDOWS下编译BOOST C++库 .

    如何在WINDOWS下编译BOOST C++库 cheungmine 2008-6-25   写出来,怕自己以后忘记了,也为初学者参考.使用VC8.0和boost1.35.0.   1)下载boost ...

  5. Windows下如何使用BOOST C++库 .

    Windows下如何使用BOOST C++库 我采用的是VC8.0和boost_1_35_0.自己重新编译boost当然可以,但是我使用了 http://www.boostpro.com/produc ...

  6. Boost线程库学习笔记

    一.创建一个线程 创建线程 boost::thread myThread(threadFun); 需要注意的是:参数可以是函数对象或者函数指针.并且这个函数无参数,并返回void类型. 当一个thre ...

  7. Boost正则表达式库regex常用search和match示例 - 编程语言 - 开发者第2241727个问答

    Boost正则表达式库regex常用search和match示例 - 编程语言 - 开发者第2241727个问答 Boost正则表达式库regex常用search和match示例 发表回复   Boo ...

  8. Boost::thread库的使用

    阅读对象 本文假设读者有几下Skills [1]在C++中至少使用过一种多线程开发库,有Mutex和Lock的概念. [2]熟悉C++开发,在开发工具中,能够编译.设置boost::thread库. ...

  9. 一起学习Boost标准库--Boost.StringAlgorithms库

    概述 在未使用Boost库时,使用STL的std::string处理一些字符串时,总是不顺手,特别是当用了C#/Python等语言后trim/split总要封装一个方法来处理.如果没有形成自己的com ...

  10. 一起学习Boost标准库--Boost.texical_cast&format库

    今天接续介绍有关字符串表示相关的两个boost库: lexical_cast 将数值转换成字符串 format 字符串输出格式化 首先,介绍下lexical_cast ,闻其名,知其意.类似C中的at ...

随机推荐

  1. Twitter的SnowFlake分布式id生成算法

    二进制相关知识回顾 1.所有的数据都是以二进制的形式存储在硬盘上.对于一个字节的8位到底是什么类型 计算机是如何分辨的呢? 其实计算机并不负责判断数据类型,数据类型是程序告诉计算机该如何解释内存块. ...

  2. python之Tkinter控件学习

    转载自  http://www.cnblogs.com/kaituorensheng/p/3287652.html#_label0 阅读目录 1. 产品介绍 2. 设计规划 3. 相关知识 4. 源码 ...

  3. jpa中时间戳格式应该用哪种类型

    遇到个bug,数据库时间存储用了datetime,但是下面的java jpa代码,查询回来,却只有日期. String innerSql = getInnerQuery(departmentId, k ...

  4. Linux下应急工具

    Linux下的应急工具 在Linux下,应急的查看点无非那个几个,一是看表现(宕机.高CPU.高内存.高IO.高网络通信),二看连接.三看进程.四看日志.五看文件(Linux一切皆文件),再者结合起来 ...

  5. 简易扩展Visual Studio UnitTesting支持TestMethodCase

    NUnit的TestCaseAttribute可以简化大量的测试参数输入用例的编写,如果基于Visual Studio Unit Test Project开发则默认没有类似的功能,看一段对比代码: p ...

  6. [转]理解Linux的处理器负载均值

    [转自]http://www.mike.org.cn/articles/understanding-of-linux-processor-load-average/ 你可能对于Linux的负载均值(l ...

  7. Android开发小技巧之根据position判断ListView是否在显示

    使用ListView的时候,会有判断指定项是否正在显示的需求,其实很简单,代码如下: private boolean isShowing(int position) { int showViewCou ...

  8. 分享10款效果惊艳的HTML5图片特效【转】

    先插入一条广告,博主新开了一家淘宝店,经营自己纯手工做的发饰,新店开业,只为信誉!需要的亲们可以光顾一下!谢谢大家的支持!店名: 小鱼尼莫手工饰品店经营: 发饰.头花.发夹.耳环等(手工制作)网店: ...

  9. openstack 部署(Q版)-----keystone认证服务安装配置

    一.新建数据库及用户 CREATE DATABASE keystone; GRANT ALL PRIVILEGES ON keystone.* TO 'keystone'@'localhost' ID ...

  10. Codeforces 592D - Super M - [树的直径][DFS]

    Time limit 2000 ms Memory limit 262144 kB Source Codeforces Round #328 (Div. 2) Ari the monster is n ...