`

shared_ptr(new)

 
阅读更多
#include <memory>
#include <string>
#include <iostream>
using namespace std;

int main()
{
	shared_ptr<string> p = make_shared<string>("a");
	cout << "p use_count:" << p.use_count() << endl;
	string *p1 = p.get();
	cout << "*p:" << *p << ",*p1:" << *p1 << endl;
	cout << "p use_count:" << p.use_count() << endl;
	auto p2 = p;
	cout << "p use_count:" << p.use_count() << endl;
	shared_ptr<string> p3(p);
	cout << "p use_count:" << p.use_count() << endl;
	shared_ptr<string> p4 = make_shared<string>("b");
	p3 = p4;
	cout << "p use_count:" << p.use_count() << endl;
	p2.~shared_ptr();//...
	cout << "p use_count:" << p.use_count() << endl;
	if(p.unique()){
		cout << "p is unique";
	}
}

p use_count:1
*p:a,*p1:a
p use_count:1
p use_count:2
p use_count:3
p use_count:2
p use_count:1
p is unique
分享到:
评论

相关推荐

    shared_ptr只能对new的内存操作

    shared_ptr只能对new的内存操作.基本知识。

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

    最近看代码,智能指针用的比较多,自己平时用的少,周末自己总结总结。...std::shared_ptr&lt;Test&gt; p1(new Test); std::shared_ptr&lt;Test&gt; p2(new Test); p1 = p2; (4) 引用计数加一/减一操作是原子性的,所

    C++智能指针shared_ptr分析

    C++智能指针shared_ptr分析 概要: shared_ptr是c++智能指针中适用场景多,功能实现较多的智能指针。它采取引用计数的方法来实现释放指针所指向的资源。下面是我代码实现的基本功能。 实例代码: template class ...

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

    通过 shared_ptr 的构造函数,可以让 shared_ptr 对象托管⼀个 new 运算符返回的指针,写法如下: 此后,ptr 就可以像 T* 类型的指针⼀样使⽤,即 *ptr 就是⽤ new 动态分配的那个对象。 多个 shared_ptr 对象可以...

    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...

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

    int main() { std::shared_ptr&lt;Person&gt; p1(new Person(1));// Person(1)的引⽤计数为1 std::shared_ptr&lt;Person&gt; p2 = std::make_shared(2); p1.reset(new Person(3));// ⾸先⽣成新对象,然后引⽤计数减1,引⽤计数...

    C++智能指针shared-ptr讲解与使用.pdf

    C++智能指针shared_ptr讲解与使⽤ ⼿动管理的弊端 在简单的程序中,我们不⼤可能忘记释放 new 出来的指针,但是随着程序规模的增⼤,我们忘了 delete 的概率也随之增⼤。在 C++ 中 new 出来的指针,赋值意味着引⽤的...

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

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

    C++智能指针.pdf

    也可以使⽤直接初始化的⽅式 shared_ptr&lt;int&gt; pint(new int(100)) 来创建⼀个 shared_ptr 并初始化,但是由于 shared_ptr 定义 的构造函数是 explicit 的,因此不能使⽤ shared_ptr&lt;int&gt; pint = new int(100) 来...

    智能指针类型转换.pdf

    shared_ptr&lt;std::exception&gt; sp1(new bad_exception("error")); 02. shared_ptr&lt;bad_exception&gt; sp2 = dynamic_pointer_cast&lt;bad_exception&gt;(sp1); 03. shared_ptr&lt;std::exception&gt; sp3 = static_pointer_cast(sp2)...

    C++智能指针详解(1).pdf

    当shared计数为0时,则证明所有指向同⼀处资源的shared_ptr们全都释放了,则随即释放该资源(哦,还会释放new出来的 SharedPtrControlBlock)。 //shared计数放在这个结构体⾥⾯,实际上结构体⾥还应该有另⼀个weak...

    C++ 解环引用智能指针

    一、使用方法: 1、 将shared_ptr.hpp,shared_ptr.cpp加入 到项目工程中; 2、 如果不需要支持多线程,可以在 "shared_ptr.hpp"文件最开始处...2、 new使用方法上不太标准,如:shared_ptr&lt;B&gt; pb=new(ydsh,(B*)0) B;

    C++智能指针循环引用问题分析.pdf

    分别是shared_ptr、weak_ptr和unique_ptr 智能指针的作⽤ 智能指针可以帮助我们管理动态分配的堆内存,减少内存泄漏的可能性 ⼿动管理堆内存有引起内存泄漏的可能,⽐如这段代码 try { int* p = new int; // Do ...

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

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

    C++11unique-ptr智能指针详解(1).pdf

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

    C++智能指针详解.pdf

    #include &lt;boost/shared_ptr.hpp&gt; class CBase: public boost::enable_shared_from_this&lt;CBase&gt; { public: virtual void f(){}//必须有个虚函数才能向上向下转换。 } typedef boost::shared_ptr&lt;CBase&gt; CBasePtr; ...

    C++智能指针的原理和实现.pdf

    ⼆、智能指针类型 ⼆、智能指针类型 智能指针在C++11版本之后提供,包含在头⽂件中,标准命名std空间下,有auto_ptr、shared_ptr、weak_ptr、unique_ptr四 种,其中auto_ptr已被弃⽤。 :拥有严格对象所有权语义的...

    C++智能指针原理.pdf

    先看auto_ptr,看⼀个⼩例⼦: auto_ptr&lt;string&gt; films[2] = { auto_ptr&lt;string&gt; (new string("Fowl Balls")), auto_ptr&lt;string&gt; (new string("Duck Walks")) }; auto_ptr&lt;string&gt; pwin; pwin = films[0];//注:...

    C++智能指针用法详解

    一、简介  由于 C++ 语言没有自动...包括:std::auto_ptr、boost::scoped_ptr、boost::shared_ptr、boost::scoped_array、boost::shared_array、boost::weak_ptr、boost:: intrusive_ptr。你可能会想,如此多的智能指

    C++智能指针(1).pdf

    C++ 中有四种智能指针:auto_pt、unique_ptr、shared_ptr、weak_ptr 其中后三个是 C++11 ⽀持,第⼀个已经被 C++11 弃⽤且被 unique_prt 代替,不推荐使⽤。下⽂将对其逐个说明。 std::auto_ptr 在这个年代讨论 std...

Global site tag (gtag.js) - Google Analytics