`
soulwzy
  • 浏览: 14894 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
最近访客 更多访客>>
社区版块
存档分类
最新评论

Boost::Shared_ptr初学

 
阅读更多
#include "boost/shared_ptr.hpp"
#include <vector>
#include <iostream>


//boost的shared_ptr 是一种智能指针,
//可以根据其被引用的次数进行智能释放,当被引用次数为0的时候自动释放,
//这个功能和iphone上面编程,objective-c里的功能有所类似。


class shared
{
private:
	boost::shared_ptr<int> p ;    //声明一个成员,智能指针p,我们将准备让它指向一个int型的空间

public:
	shared(boost::shared_ptr<int> p_):p(p_){}    //构造函数(另一种构造方式)

	void print()
	{
		std::cout<<"count:"<<p.use_count()<<"v="<<*p<<std::endl;  //use_count即被引用次数
	}
};


void print_func(boost::shared_ptr<int> p)
{
	std::cout<<"count:"<<p.use_count()<<"v="<<*p<<std::endl;
}
 

int main()
{
	boost::shared_ptr<int> p(new int(100));

	std::cout<<p.use_count()<<std::endl;     //刚被创建的时候,引用次数为1

	shared s1(p),s2(p);   //直接值传递进行拷贝,拷贝两次,引用次数多到3
	s1.print();
	s2.print();

	*p=20;  //改变所指向空间的内容
 	print_func(p);    //这里又有一次值传递,所以这里引用次数达到4
	s1.print();    //上面的函数执行完之后,退出,所以引用次数又回到3

}

分享到:
评论

相关推荐

    C++11 std::shared_ptr总结与使用示例代码详解

    std::shared_ptr大概总结有以下几点: (1) 智能指针主要的用途就是方便资源的管理,自动释放没有指针引用的资源。 (2) 使用引用计数来标识是否有多余指针指向该资源。(注意,shart_ptr本身指针会占1个引用) (3) 在...

    shared_from_this() in Constructor:直接替换std :: shared_ptr + std :: enable_shared_from_this-开源

    显然,许多人不喜欢标准std :: enable_... boost库也可以这样做,但是它不允许在析构函数中创建shared_ptrs,并且它不提供release()方法来获取所包含指针的所有权。 但是,不利的一面是,它还没有成为线程安全的。

    C++ 智能指针(shared_ptr/weak_ptr)源码

    C++ 智能指针(shared_ptr/weak_ptr)源码 源码位置:gcc-6.1.0\gcc-6.1.0\libstdc++-v3\include\tr1 这里只单列shared_ptr.h文件用于分析

    shared_ptr for arm

    shared_ptr arm, use arm asm code

    shared_ptr

    shared_ptr boost audio video AVDataPool

    用shared_ptr实现多线程对全局变量的读写,copy-on-write技术

    参考陈硕的多线程服务端编程&gt;&gt;,中的用shared_ptr实现copy-on-write技术,不过这里的线程采用的是c++11的线程库

    智能指针shared_ptr的Demo

    这是一个演示智能指针shared_ptr的一个Demo。本代码所对应的博客地址为https://blog.csdn.net/wfh2015/article/details/80699378

    shared_ptr线程安全性全面分析

    shared_ptr的线程安全性boost官方文档对shared_ptr线程安全性的正式表述是:shared_ptr对象提供与内置类型相同级别的线程安全性。【shared_ptrobjects offer the same level of thread safety as built-in types.】...

    浅析Boost智能指针:scoped_ptr shared_ptr weak_ptr

    scoped_ptrboost::scoped_ptr和std::auto_ptr非常类似,是一个简单的智能指针,它能够保证在离开作用域后对象被自动释放。下列代码演示了该指针的基本应用: 代码如下:#include &lt;string&gt;#include &lt;iostream&gt;#...

    C++11新特性之智能指针(shared_ptr/unique_ptr/weak_ptr)

    shared_ptr基本用法 shared_ptr采用引用计数的方式管理所指向的对象。当有一个新的shared_ptr指向同一个对象时(复制shared_ptr等),引用计数加1。当shared_ptr离开作用域时,引用计数减1。当引用计数为0时,释放所...

    C++ unique_ptr weak_ptr shared_ptr auto_ptr智能指针.doc

    四种智能指针的使用、机制和缺陷分析

    C++智能指针-unique-ptr智能指针详解.pdf

    和 shared_ptr 指针最⼤的不同之处在 于,unique_ptr 指针指向的堆内存⽆法同其它 unique_ptr 共享,也就是说,每个 unique_ptr 指针都独⾃拥有对其所指堆内存空间的所有 权。 这也就意味着,每个 unique_ptr 指针...

    shared-ptr(智能指针)举例.pdf

    shared_ptr(智能指针)举例 程序使⽤动态内存出于以下三种原因之⼀ : 1、程序不知道⾃⼰需要多少对象; 2、程序不知道所需对象的准确类型; 3、程序需要在多个对象间共享数据 ⼀. 要确保⽤ new 动态分配的内存空间...

    智能指针shared-ptr的用法.pdf

    shared_ptr的基本⽤法 初始化 可以通过构造函数、std::make_shared辅助函数和reset⽅法来初始化shared_ptr: #include "stdafx.h" #include &lt;iostream&gt; #include &lt;future&gt; #include &lt;thread&gt; using namespace std;...

    C++11 智能指针之shared_ptr代码详解

    C++中的智能指针首先出现在“准”标准库boost中。 随着使用的人越来越多,为了让开发人员更方便、更安全的使用动态内存,C++11也引入了智能指针来管理动态对象。 在新标准中,主要提供了shared_ptr、unique_ptr、...

    C++智能指针shared_ptr分析

    C++智能指针shared_ptr分析 概要: shared_ptr是c++智能指针中适用场景多,功能实现较多的... :_ptr(ptr) , _refCount(new int(1)) {} sharedptr(sharedptr&lt;T&gt;& sp) :_ptr(sp._ptr) , _refCount(sp._refCount) {

    C++智能指针:shared-ptr用法详解.pdf

    C++智能指针:shared_ptr⽤法详解 C++智能指针:shared_ptr⽤法详解 shared_ptr是C++11⾥的新特性,其包装了new操作符在堆上分配的动态对象。如: shared_ptr&lt;int&gt; sp1(new int(100)); //相当于 //int *sp1=new int...

Global site tag (gtag.js) - Google Analytics