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的更多相关文章

  1. [5] 智能指针boost::shared_ptr

    [1]boost::shared_ptr简介 boost::shared_ptr属于boost库,定义在namespace boost中,包含头文件#include<boost/shared_p ...

  2. 记录以下boost::shared_ptr的一个使用细节

    shared_ptr<T>::operator->返回的是T*类型指针,非const T*指针.因此通过const shared_ptr<T>&类型的ptr可以直 ...

  3. 关于智能指针boost::shared_ptr

    boost库中的智能指针shared_ptr, 功能强大, 且开销小,故受到广大coder的欢迎. 但在实际的使用过程中,笔者也发现了一些不足. 1.定制的删除器 shared_ptr除了可以使用默认 ...

  4. #include <boost/shared_ptr.hpp>

    共享指针 这个智能指针命名为boost::shared_ptr,定义在boost/shared_ptr.hpp里.智能指针boost::shared_ptr基本上类似于boost::scoped_pt ...

  5. 智能指针剖析(下)boost::shared_ptr&其他

    1. boost::shared_ptr 前面我已经讲解了两个比较简单的智能指针,它们都有各自的优缺点.由于 boost::scoped_ptr 独享所有权,当我们真真需要复制智能指针时,需求便满足不 ...

  6. C++智能指针剖析(下)boost::shared_ptr&其他

    1. boost::shared_ptr 前面我已经讲解了两个比较简单的智能指针,它们都有各自的优缺点.由于 boost::scoped_ptr 独享所有权,当我们真真需要复制智能指针时,需求便满足不 ...

  7. 【C++】boost::shared_ptr boost::make_shared

    一.shared_ptr shared_ptr作为一个动态分配的对象,当最后一个指向其内容的指针销毁(destroyed)或重置(reset),其指向的内容会被销毁(deleted).不再需要显式调用 ...

  8. 智能指针tr1::shared_ptr、boost::shared_ptr使用

    对于tr1::shared_ptr在安装vs同一时候会自带安装,可是版本号较低的不存在.而boost作为tr1的实现品,包括 "Algorithms Broken Compiler Work ...

  9. boost shared_ptr weak_ptr

    文档: http://www.boost.org/doc/libs/1_57_0/libs/smart_ptr/shared_ptr.htm shared_ptr构造有个原型 template< ...

随机推荐

  1. 牛客小白月赛15 C 表单 ( map 使用)

    链接:https://ac.nowcoder.com/acm/contest/917/C来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言52428 ...

  2. Java中多个线程交替循环执行

    有些时候面试官经常会问,两个线程怎么交替执行呀,如果是三个线程,又怎么交替执行呀,这种问题一般人还真不一定能回答上来.多线程这块如果理解的不好,学起来是很吃力的,更别说面试了.下面我们就来剖析一下怎么 ...

  3. Java中JPS命令监控

    很多人在学习java的时候只是对java粗略的学了一遍,很少有人能了解jvm层面的一些东西,比如我们想看目前有多少个java进程,可以在命令行执行jps.下面我们来说说jps的一些详细的用法. jps ...

  4. 题解报告:hdu 1028 Ignatius and the Princess III(母函数or计数DP)

    Problem Description "Well, it seems the first problem is too easy. I will let you know how fool ...

  5. 【Leetcode 3】Longest Substring Without Repeating Characters0

    Description: Given a string, find the length of the longest substring without repeating characters. ...

  6. Unity学习-软件的基本操作(二)

    基本操作 1:Scene中 以小手显示,平移画布,与鼠标中键一样 2:平移 游戏对象,组件的 Position属性 也可设置 3:旋转 游戏对象,组件的 Rotation属性 也可设置 4:缩放 游戏 ...

  7. 关于debug.keystore文件用法以及错误处理

    在开发过程中需要频繁的为测试的同事签名apk,非常很麻烦,把默认debug.keystore文件替换成发布用(生产环境)的签名文件,不用频繁地签名apk文件了.      如果直接使用生产keysto ...

  8. hibernate映射数据库时@ManyToOne和@OneToMany

    第一次用hibernate自动生成表,涉及到多个表的外键,用到了@OneToMany和@ManyToOne注解碰到了几个错误. 首先声明一个基础,@OneToMany和@ManyToOne两个注解没有 ...

  9. JavaWeb项目中引入spring框架

    主要步骤有以下3步: 1:下载spring的jar包2:在项目中web.xml中添加spring配置3:bean配置文件-applicationContext.xml 1:引入包,这个就不说了,官网下 ...

  10. CSS——text-indent

    在h1标签里套入a标签并写上文字,有利于seo,但是文字如何隐藏呢?一般都是a标签变成内联块并首行缩进为负值. <!DOCTYPE html> <html lang="en ...