boost::shared_ptr
boost::shared_ptr是boost库中用来管理指针的模板,使用它需要#include <boost/shared_ptr.hpp>。本文介绍它的一些基本用法。
第一,boost::shared_ptr管理的指针所指向的对象必须在堆中,因为该模板会在对象离开作用域后调用delete方法,如果对象位于栈中,程序编译能通过,但在运行中会崩溃。另外改模板提供了swap方法,可以让两个模板指针相互交换所指向的对象。
#include <vector>
#include <set>
#include <iostream>
#include <algorithm>
#include <boost/shared_ptr.hpp> // The application will produce a series of
// objects of type Foo which later must be
// accessed both by occurrence (std::vector)
// and by ordering relationship (std::set). struct Foo
{
Foo( int _x ) : x(_x) {}
~Foo() { std::cout << "Destructing a Foo with x=" << x << "\n"; }
int x;
/* ... */
}; typedef boost::shared_ptr<Foo> FooPtr; typedef struct FooPtrOps
{
bool operator()( const FooPtr & a, const FooPtr & b )
{ return a->x > b->x; }
void operator()( const FooPtr & a )
{ std::cout << a->x << "\n"; }
} foo_ptr_ops; foo_ptr_ops ins_foo_ptr_ops1 = foo_ptr_ops();
FooPtrOps *ins_foo_ptr_ops2 = new FooPtrOps();
FooPtrOps *ins_foo_ptr_ops3 = new foo_ptr_ops; int main()
{
{
std::vector<FooPtr> foo_vector;
std::set<FooPtr,FooPtrOps> foo_set; // NOT multiset! FooPtr foo_ptr( new Foo( ) );
foo_vector.push_back( foo_ptr );
foo_set.insert( foo_ptr ); foo_ptr.reset( new Foo( ) );
foo_vector.push_back( foo_ptr );
foo_set.insert( foo_ptr ); foo_ptr.reset( new Foo( ) );
foo_vector.push_back( foo_ptr );
foo_set.insert( foo_ptr ); foo_ptr.reset ( new Foo( ) );
foo_vector.push_back( foo_ptr );
foo_set.insert( foo_ptr ); std::cout << "foo_vector:\n";
std::for_each( foo_vector.begin(), foo_vector.end(), ins_foo_ptr_ops1 ); std::cout << "\nfoo_set:\n";
std::for_each( foo_set.begin(), foo_set.end(), *ins_foo_ptr_ops3 ); FooPtr foo_ptr1( new Foo( ) );
FooPtr foo_ptr2( new Foo( ) );
std::cout << "foo_ptr1: " << foo_ptr1->x << '\n';
std::cout << "foo_ptr2: " << foo_ptr2->x << '\n'; foo_ptr1.swap(foo_ptr2);
std::cout << "After swap:\n";
std::cout << "foo_ptr1: " << foo_ptr1->x << '\n';
std::cout << "foo_ptr2: " << foo_ptr2->x << '\n'; foo_ptr2.swap(foo_ptr1);
std::cout << "Swap again:\n";
std::cout << "foo_ptr1: " << foo_ptr1->x << '\n';
std::cout << "foo_ptr2: " << foo_ptr2->x << '\n'; int a = ;
int b[] = {, , , };
int *c = new int();
int *d = new int[]; /*
* Because variable a and b are on stack, while boost::shared_ptr will call delete method,
* the following two rows of code will cause error.
*/
// boost::shared_ptr<int> bsa(&a); // Error: Signal: SIGABRT (Aborted)
// boost::shared_ptr<int> bsb(b); // Error: Signal: SIGABRT (Aborted)
boost::shared_ptr<int> bsc(c);
boost::shared_ptr<int> bsd(d);
std::cout << "bsc: " << *bsc << " bsd: " << *bsd << std::endl; std::cout << "The variable field finished." << "\n";
} int *c = new int();
int *d = new int[];
boost::shared_ptr<int> bsc(c);
boost::shared_ptr<int> bsd(d);
std::cout << "bsc: " << *bsc << " bsd: " << *bsd << std::endl; std::cout << "\nProgram done.\n";
}
程序的运行结果:
foo_vector: foo_set: foo_ptr1:
foo_ptr2:
After swap:
foo_ptr1:
foo_ptr2:
Swap again:
foo_ptr1:
foo_ptr2:
bsc: bsd:
The variable field finished.
Destructing a Foo with x=
Destructing a Foo with x=
Destructing a Foo with x=
Destructing a Foo with x=
Destructing a Foo with x=
Destructing a Foo with x=
bsc: bsd: Program done.
第二,boost::shared_ptr支持隐藏类的定义。如下面的代码中,class implementation的定义可以放置于另一个源文件中,在利用boost::shared_ptr管理implementation类型的指针变量时,可以先声明一下类implementation,然后就能定义boost::shared_ptr< implementation >类型的指针变量。
#include <boost/shared_ptr.hpp>
#include <iostream>
#include <algorithm> void print_val(int v)
{
std::cout << v << " ";
} class example
{
public:
example();
void do_something();
int val[];
class implementation;
boost::shared_ptr< implementation > _imp; // hide implementation details
}; class example::implementation
{
public:
~implementation() { std::cout << "destroying implementation\n"; }
}; example::example() : _imp( new implementation ) {} void example::do_something()
{
std::cout << "use_count() is " << _imp.use_count() << " ";
std::for_each(val, val + , print_val);
std::cout << "\n";
} int main()
{
example a;
a.val[] = ;
a.val[] = ;
a.val[] = ;
a.do_something();
example b(a);
b.do_something();
example c;
c = a;
a.do_something();
b.do_something();
c.do_something();
return ;
}
程序的运行结果:
use_count() is
use_count() is
destroying implementation
use_count() is
use_count() is
use_count() is
destroying implementation
第三,使用boost::shared_ptr提供的reset()方法,可以使boost::shared_ptr管理的指针所指向的对象的引用计数减一。当所指对象的引用计数减至0时,所指对象的析构函数将被调用,所指对象被销毁。
#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> myBook1(new Book("「1984」"));
cout << "myBook1: " << myBook1.use_count() << endl;
boost::shared_ptr<Book> myBook2(myBook1);
cout << "myBook1: " << myBook1.use_count() << endl;
boost::shared_ptr<Book> myBook3;
myBook3 = myBook1; cout << "\n****************************\n";
cout << "myBook1: " << myBook1.use_count() << endl;
cout << "myBook2: " << myBook2.use_count() << endl;
cout << "myBook3: " << myBook3.use_count() << endl; cout << "\n****************************\n";
myBook1.reset();
cout << "myBook1: " << myBook1.use_count() << endl;
cout << "myBook2: " << myBook2.use_count() << endl;
cout << "myBook3: " << myBook3.use_count() << endl; cout << "\n****************************\n";
myBook3.reset();
cout << "myBook1: " << myBook1.use_count() << endl;
cout << "myBook2: " << myBook2.use_count() << endl;
cout << "myBook3: " << myBook3.use_count() << endl; cout << "\n****************************\n";
myBook2.reset();
cout << "myBook1: " << myBook1.use_count() << endl;
cout << "myBook2: " << myBook2.use_count() << endl;
cout << "myBook3: " << myBook3.use_count() << endl; cout << "After reset ..." << endl;
}
cout << "===== Main End =====" << endl; return ;
}
程序的运行结果:
=====Main Begin=====
Creating book 「」 ...
myBook1:
myBook1: ****************************
myBook1:
myBook2:
myBook3: ****************************
myBook1:
myBook2:
myBook3: ****************************
myBook1:
myBook2:
myBook3: ****************************
Destroying book 「」 ...
myBook1:
myBook2:
myBook3:
After reset ...
===== Main End =====
boost::shared_ptr的更多相关文章
- [5] 智能指针boost::shared_ptr
[1]boost::shared_ptr简介 boost::shared_ptr属于boost库,定义在namespace boost中,包含头文件#include<boost/shared_p ...
- 记录以下boost::shared_ptr的一个使用细节
shared_ptr<T>::operator->返回的是T*类型指针,非const T*指针.因此通过const shared_ptr<T>&类型的ptr可以直 ...
- 关于智能指针boost::shared_ptr
boost库中的智能指针shared_ptr, 功能强大, 且开销小,故受到广大coder的欢迎. 但在实际的使用过程中,笔者也发现了一些不足. 1.定制的删除器 shared_ptr除了可以使用默认 ...
- #include <boost/shared_ptr.hpp>
共享指针 这个智能指针命名为boost::shared_ptr,定义在boost/shared_ptr.hpp里.智能指针boost::shared_ptr基本上类似于boost::scoped_pt ...
- 智能指针剖析(下)boost::shared_ptr&其他
1. boost::shared_ptr 前面我已经讲解了两个比较简单的智能指针,它们都有各自的优缺点.由于 boost::scoped_ptr 独享所有权,当我们真真需要复制智能指针时,需求便满足不 ...
- C++智能指针剖析(下)boost::shared_ptr&其他
1. boost::shared_ptr 前面我已经讲解了两个比较简单的智能指针,它们都有各自的优缺点.由于 boost::scoped_ptr 独享所有权,当我们真真需要复制智能指针时,需求便满足不 ...
- 【C++】boost::shared_ptr boost::make_shared
一.shared_ptr shared_ptr作为一个动态分配的对象,当最后一个指向其内容的指针销毁(destroyed)或重置(reset),其指向的内容会被销毁(deleted).不再需要显式调用 ...
- 智能指针tr1::shared_ptr、boost::shared_ptr使用
对于tr1::shared_ptr在安装vs同一时候会自带安装,可是版本号较低的不存在.而boost作为tr1的实现品,包括 "Algorithms Broken Compiler Work ...
- boost shared_ptr weak_ptr
文档: http://www.boost.org/doc/libs/1_57_0/libs/smart_ptr/shared_ptr.htm shared_ptr构造有个原型 template< ...
随机推荐
- [Swift通天遁地]六、智能布局-(7)通过Group(组)命令实现对多个视图的统一约束
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- AcWing算法基础1.2
排序 归并排序 归并排序和快速排序相反,快排是先排后分再合并,归并则是先分后排再合并 归并排序时间复杂度是O(n logn) 分析: ------------------------------ ...
- Sublime Text3 配置 Lua5.3.5开发环境
所需软件 Sublime Text3 Lua5.3.5 配置过程 解压Lua5.3.5包 官方下载的包内是需要makefile安装的(博主Win10下暂为实现),此处提供自动配置完毕的包:Lua5.3 ...
- IDEA报错,注解标红,提示Cannot resolve symbol xxx
一般都是jar包没导进来,可以先看一下setting里maven配置的路径对不对
- SpringBoot集成CAS单点登录,SSO单点登录,CAS单点登录(视频资料分享篇)
单点登录(Single Sign On),简称为 SSO,是目前比较流行的企业业务整合的解决方案之一.SSO的定义是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统. 很早期的公司 ...
- C# 针对文件夹的操作
//创建文件夹Directory.CreateDirectory(Server.MapPath("a"));Directory.CreateDirectory(Server.Map ...
- x264
x264 x264 h264 1. x264调用主要过程 x264_param_default():设置参数集结构体x264_param_t的缺省值. x264_picture_alloc():为 ...
- jQuery实现页面锚点滚动效果
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- jQuery——属相操作
属性获取:attr(属性名), 属性设置:attr(属性名,具体值) 移除属性:removeAttr(属性名) 特殊情况:prop(属性名).prop(属性名,具体值):表单中状态属性checked. ...
- Matrix computations in C
meschach配置使用 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !im ...