boost相关函数
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相关函数的更多相关文章
- 【Boost】boost::string_algo详解2——find相关函数
来自: https://blog.csdn.net/huang_xw/article/details/8276123 函数声明: template<typename Range1T, typ ...
- Boost使用笔记(Smart_ptr)
我是Word写的,复制过来实在懒得在排版了,有兴趣的朋友可以去我的百度文库看,谢谢 http://wenku.baidu.com/view/34e485e2f61fb7360b4c653e.html ...
- (九)boost库之文件处理filesystem
(九)boost库之文件处理filesystem filesystem库是一个可移植的文件系统操作库,它在底层做了大量的工作,使用POSIX标准表示文件系统的路径,使C++具有了类似脚本语言的功能 ...
- boost signal2 slot_base
先看成员_tracked_objects,从字面上讲是被跟踪的对象,再看,相关函数 bool expired() const,这个函数是检查_tracked_objects是否已经expired.只不 ...
- [Boost]boost的时间和日期处理-(1)日期的操作
<开篇> Boost.DateTime库提供了时间日期相关的计算.格式化.转换.输入输出等等功能,为C++的编程提供了便利.不过它有如下特点: 1. Boost.DateTime 只支持1 ...
- boost.asio系列——buffer
创建buffer 在io操作中,对数据的读写大都是在一个缓冲区上进行的,在asio框架中,可以通过asio::buffer函数创建一个缓冲区来提供数据的读写.buffer函数本身并不申请内存,只是提供 ...
- C++使用BOOST操作文件、目录
开始使用 在BOOST库出现之前,C++对于文件和目录的操作,大都借助于UNIX提供的底层文件和目录接口,从使用角度来看,这些底层的操作不够友好.BOOST中filesystem库是一种可移植的文件系 ...
- Boost总结汇总
从开始接触Boost已经有好几年了,而对它的掌握却难言熟悉,有对它部分的源代码的剖析也是蜻蜓点水.有时间一点点梳理一下吧. 1. 概述 [Boost]C++ Boost库简介[Boost]C++ Bo ...
- boost强分类器的实现
boost.cpp文件下: bool CvCascadeBoost::train( const CvFeatureEvaluator* _featureEvaluator, int _numSampl ...
随机推荐
- POJ 3468 A Simple Problem with Integers (分块)
Description You have \(N\) integers, \(A_1, A_2, ... , A_N\). You need to deal with two kinds of ope ...
- Java 自动检测文本文件编码
private String guessCharset(InputStream is) throws IOException { return new TikaEncodingDetector().g ...
- [已解决]报错run `npm audit fix` to fix them, or `npm audit` for details
问题: added 246 packages from 681 contributors and audited 382 packages in 17.509s found 13 vulnerabil ...
- swagger2 注解说明 ( @ApiImplicitParams )
@Api:用在请求的类上,表示对类的说明 tags="说明该类的作用,可以在UI界面上看到的注解" value="该参数没什么意义,在UI界面上也看到,所以不需要配置&q ...
- 装箱与拆箱(TDB)
装箱:把值类型转换为引用类型 拆箱:把引用类型转换为值类型 只能对之前装箱的变量进行拆箱.需要强制转换.
- 2019-10-10-dotnet-新-sdk-style-项目格式的一些命名空间和引用
title author date CreateTime categories dotnet 新 sdk style 项目格式的一些命名空间和引用 lindexi 2019-10-10 10:6:46 ...
- 基于nginx结合openssl实现https
[root@localhost ~]#systemctl stop firewalld[root@localhost ~]#setenforce 0[root@localhost ~]#iptable ...
- 基本TCP Sockets编程
一.socket 函数 #include <sys/socket.h> int socket (int family, int type, int protocol); Returns: ...
- final和abstract关键字的作用
final和abstract关键字的作用 final和abstract是功能相反的两个关键字,可以对比记忆 abstract可以用来修饰类和方法,不能用来修饰属性和构造方法:使用abstract修饰的 ...
- JUC 一 ReentrantLock 可重入锁
java.util.concurrent.locks ReentrantLock即可重入锁,实现了Lock和Serializable接口 ReentrantLock和synchronized都是可重入 ...