1、boost::scoped_ptr是一个比较简单的智能指针,它能保证在离开作用域之后它所管理对象能被自动释放

 #include <iostream>
#include <boost/scoped_ptr.hpp> using namespace std; class Book
{
public:
Book()
{
cout << "Creating book ..." << endl;
} ~Book()
{
cout << "Destroying book ..." << endl;
}
}; int main()
{
cout << "=====Main Begin=====" << endl;
{
boost::scoped_ptr<Book> myBook(new Book());
}
cout << "===== Main End =====" << endl; return ;
}

2、 boost::shared_ptr是可以共享所有权的指针。如果有多个shared_ptr共同管理同一个对象时,只有这些shared_ptr全部与该对象脱离关系之后,被管理的对象才会被释放。通过下面这个例子先了解下shared_ptr的基本用法:。

 #include <iostream>
#include <string>
#include <boost/shared_ptr.hpp> using namespace std; class Book
{
private:
string name_; public:
Book(string name) : name_(name)
{
cout << "Creating book " << name_ << " ..." << endl;
} ~Book()
{
cout << "Destroying book " << name_ << " ..." << endl;
}
}; int main()
{
cout << "=====Main Begin=====" << endl;
{
boost::shared_ptr<Book> myBook(new Book("「1984」"));
cout << "[From myBook] The ref count of book is " << myBook.use_count() << ".\n" << endl; boost::shared_ptr<Book> myBook1(myBook);
cout << "[From myBook] The ref count of book is " << myBook.use_count() << "." << endl;
cout << "[From myBook1] The ref count of book is " << myBook1.use_count() << ".\n" << endl; cout << "Reset for 1th time. Begin..." << endl;
myBook.reset();
cout << "[From myBook] The ref count of book is " << myBook.use_count() << "." << endl;
cout << "[From myBook1] The ref count of book is " << myBook1.use_count() << "." << endl;
cout << "Reset for 1th time. End ...\n" << endl; cout << "Reset for 2th time. Begin ..." << endl;
myBook1.reset();
cout << "Reset for 2th time. End ..." << endl;
}
cout << "===== Main End =====" << endl; return ;
}

3、shared_from_this()

  • 在一个类中需要传递类对象本身shared_ptr的地方使用shared_from_this函数来获得指向自身的shared_ptr,它是enable_shared_from_this的成员函数,返回shared_ptr。

  • 这个函数仅在shared_ptr的构造函数被调用之后才能使用。原因是enable_shared_from_this::weak_ptr并不在enable_shared_from_this构造函数中设置,而是在shared_ptr的构造函数中设置。

boost相关函数的更多相关文章

  1. 【Boost】boost::string_algo详解2——find相关函数

    来自: https://blog.csdn.net/huang_xw/article/details/8276123 函数声明:   template<typename Range1T, typ ...

  2. Boost使用笔记(Smart_ptr)

    我是Word写的,复制过来实在懒得在排版了,有兴趣的朋友可以去我的百度文库看,谢谢 http://wenku.baidu.com/view/34e485e2f61fb7360b4c653e.html ...

  3. (九)boost库之文件处理filesystem

    (九)boost库之文件处理filesystem   filesystem库是一个可移植的文件系统操作库,它在底层做了大量的工作,使用POSIX标准表示文件系统的路径,使C++具有了类似脚本语言的功能 ...

  4. boost signal2 slot_base

    先看成员_tracked_objects,从字面上讲是被跟踪的对象,再看,相关函数 bool expired() const,这个函数是检查_tracked_objects是否已经expired.只不 ...

  5. [Boost]boost的时间和日期处理-(1)日期的操作

    <开篇> Boost.DateTime库提供了时间日期相关的计算.格式化.转换.输入输出等等功能,为C++的编程提供了便利.不过它有如下特点: 1. Boost.DateTime 只支持1 ...

  6. boost.asio系列——buffer

    创建buffer 在io操作中,对数据的读写大都是在一个缓冲区上进行的,在asio框架中,可以通过asio::buffer函数创建一个缓冲区来提供数据的读写.buffer函数本身并不申请内存,只是提供 ...

  7. C++使用BOOST操作文件、目录

    开始使用 在BOOST库出现之前,C++对于文件和目录的操作,大都借助于UNIX提供的底层文件和目录接口,从使用角度来看,这些底层的操作不够友好.BOOST中filesystem库是一种可移植的文件系 ...

  8. Boost总结汇总

    从开始接触Boost已经有好几年了,而对它的掌握却难言熟悉,有对它部分的源代码的剖析也是蜻蜓点水.有时间一点点梳理一下吧. 1. 概述 [Boost]C++ Boost库简介[Boost]C++ Bo ...

  9. boost强分类器的实现

    boost.cpp文件下: bool CvCascadeBoost::train( const CvFeatureEvaluator* _featureEvaluator, int _numSampl ...

随机推荐

  1. SOA(面向服务的体系结构)

    SOA(面向服务的体系结构) 面向服务的体系结构是一个组件模型,它将应用程序的不同功能单元(称为服务)通过这些服务之间定义良好的接口和契约联系起来.接口是采用中立的方式进行定义的,它应该独立于实现服务 ...

  2. Win7环境下VS2010配置Cocos2d-x-2.1.4最新版本的开发环境(亲测)

      写这篇博客时2D游戏引擎Cocos2d-x的最新版本为2.1.4,记得很久以前使用博客园博主子龙山人的一篇博文<Cocos2d-x win7+vs2010配置图文详解(亲测)>成功配置 ...

  3. angularJS 绑定操作

    <button type="submit" ng-disabled="editForm.$invalid || vm.isSaving" class=&q ...

  4. ubuntu16.04安装python虚拟环境

    自己也是搜的教程,亲测有效 ubuntu16.04创建虚拟环境 一.linux环境 Ubuntu16.04 二.安装和配置虚拟环境 安装虚拟环境 sudo pip install virtualenv ...

  5. windows 修改远程登录端口号

    运行regedit.exe打开注册表编辑器,即在cmd的dos窗口输入regedit命令 找到如下注册表子项: HKEY_LOCAL_MACHINE\System\CurrentControlSet\ ...

  6. socket 进程通讯理解

    [转]https://blog.csdn.net/andrewgithub/article/details/81613120 正如可以给fopen的传入不同参数值,以打开不同的文件.创建socket的 ...

  7. leetcode-第11场双周赛-5088-等差数列中缺失的数字

    题目描述: 自己的提交: class Solution: def missingNumber(self, arr: List[int]) -> int: if len(arr) == 2: re ...

  8. Batch - forfiles 命令详解

    forfiles 命令详解 C:\Users\cuixunxu>forfiles /? FORFILES [/P pathname] [/M searchmask] [/S] [/C comma ...

  9. Dart编程数字Number

    Dart数字可以分为: int - 任意大小的整数. int 数据类型用于表示整数. double -64位(双精度)浮点数,由IEEE 754标准规定. 在 double 数据类型用于表示小数 in ...

  10. 【历年真题】斐波那契数列logn做法

    描述 [题解] 用矩阵乘法加速递推 [0 1] [1 1] [f[n-1]] [f[n-2]] = [f[n-1]] [f[n]] 求A矩阵的n-2次幂然后再乘B矩阵. 结果矩阵中的第二行第一列就是f ...