BOOST Library (1)

class implementation
{
public:
~implementation() { std::cout <<"destroying implementation\n"; }
void do_something() { std::cout << "did something\n"; }
};

void test()
{
boost::shared_ptr<implementation> sp1(new implementation());
std::cout<<"The Sample now has "<<sp1.use_count()<<" references\n";

boost::shared_ptr<implementation> sp2 = sp1;
std::cout<<"The Sample now has "<<sp2.use_count()<<" references\n";

sp1.reset();
std::cout<<"After Reset sp1. The Sample now has "<<sp2.use_count()<<" references\n";

sp2.reset();
std::cout<<"After Reset sp2.\n";
}

程序输出内容:
The Sample now has 1 references
The Sample now has 2 references
After Reset sp1. The Sample now has 1 references
destroying implementation
After Reset sp2.

归纳如下:
Shared_pt可以共享内存,两个Shared_ptr之间直接用“=”赋值,每个Shared_ptr内部都有一个计数器记录该内存地址有多少个Shared_ptr持有,可以用use_count()访问。当该计数器为0时,会自动调用delete删除内存。

时间: 2024-08-06 07:48:07

BOOST Library (1)的相关文章

实践篇:Install Homebrew and Boost Library 以及 Xcode配置Boost库

为什么写这篇文章? 1.在安装Homebrew的时候,我去网上找过相关资料,不过都是千篇一律,都是copy来copy去的.我按照了其中几篇博文的方法,却出现了莫名其妙的问题.于是,我写了这篇文章,来帮助与我遇到同样问题的童鞋! 2.在安装boost后,xcode的配置boost库是个大问题,网上没有较为详细教程. 1.Homebrew "Homebrew installs the stuff you need that Apple didn't."--Homebrew Homebrew

Mac下boost的安装与使用 Install and use boost library on Mac

Boost库的介绍:http://www.boost.org/ 要想在mac上使用boost库写应用程序,首先需要安装boost. 安装步骤:参考官网教程http://www.boost.org/doc/libs/1_57_0/more/getting_started/unix-variants.html 1.下载 Download boost_1_57_0.tar.bz2. 2.解压 控制台操作命名:tar --bzip2 -xf /path/to/boost_1_57_0.tar.bz2 也

Boost 1.61.0 Library Documentation

http://www.boost.org/doc/libs/1_61_0/ Boost 1.61.0 Library Documentation Accumulators Framework for incremental calculation, and collection of statistical accumulators. Author(s): Eric Niebler First Release: 1.36.0 Standard: Categories: Math and nume

C++ Development Library

C/C++ 开发库 | C/C++ Development Library 这里收集一些著名的 C/C++ 开发库.SDK.类库.可复用类与结构代码 等信息,列举它们的介绍.参考和网站链接,为各位 C/C++ 程序员和爱好者提供检索和查阅类库的方便 下面收集的 C/C++ 类库介绍整理来源于文章:C++ 资源之不完全导引(作者:曾毅.陶文),这篇文章曾发表于 2004 年 5 月<CSDN 开发高手> 上文中介绍的类库有些已经多年未见发布和网站内容的更新了,特别是一些开源的项目.我检查了作者提

Ubuntu Eclipse Boost 配置

Boost 是我直接 sudo apt-get install 的,系统会帮我安装到 usr/include/boost 和 usr/lib 下,因此我可以直接使用 -lboost_system 库, 或直接 include <boost/thread.hpp> 而不用做额外的配置 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include <boost/thread.hpp> #incl

[boost] build boost with intel compiler 16.0.XXX

Introduction There are few information about how to compile boost with Intel compiler. This article is to describe a simple command steps to let you get a boost library with Intel compiler support. Steps step 1: start your Intel compiler cmd window s

c++ 使用boost regex库 总结

用java的时候觉得挺折腾,回头来弄c++才知道什么叫折腾...汗... 首先参考我写的这篇文章:http://www.cnblogs.com/qrlozte/p/4100892.html 我从sourceforge把整个boost的zip下载下来以后,我主要是在编译 boost regex的时候出问题了:boost有很多library,regex只是其中一个,怎么编译regex? 当然,第一步是查看文档,找到boost-path/doc/html/index.html打开,翻了半天倒找rege

编译gearman提示缺少boost

编译german时提示缺少boost: checking for boostlib >= 1.39... configure: We could not detect the boost libraries (version 1.39 or higher). If you have a staged boost library (still not installed) please specify $BOOST_ROOT in your environment and do not give

Boost Thread and Synchronization Tutorial

Launching threads A new thread is launched by passing an object of a callable type that can be invoked with no parameter to the constructor. The object is then copied into internal storage, and invoked on the newly-created thread of execution. If the