shared_ptr 是一个共享所有权的智能指针,允许多个指针指向同一个对象。shared_ptr 对象除了包括一个对象的指针,还包括一个引用计数器。当每给对象分配一个share_ptr的时候,引用计数加一;每reset一个share_ptr, 或者修改对象的指向(指向其他对象或者赋值nullptr),或者局部share_ptr离开作用域,引用计数减一,当引用计数为0的时候,自动释放自己所管理的对象。

构造:

      struct C {int* data;};
    std::shared_ptr<int> p1;
std::shared_ptr<int> p2 (nullptr);
std::shared_ptr<int> p3 (new int);
std::shared_ptr<int> p4 (new int, std::default_delete<int>());
std::shared_ptr<int> p5 (new int, [](int* p){delete p;}, std::allocator<int>());
std::shared_ptr<int> p6 (p5);
std::shared_ptr<int> p7 (std::move(p6));
std::shared_ptr<int> p8 (std::unique_ptr<int>(new int));
std::shared_ptr<C> obj (new C);
std::shared_ptr<int> p9 (obj, obj->data);

输出引用计数:

拷贝和转移:

std::shared_ptr<int> foo;
std::shared_ptr<int> bar(new int());
foo = bar; // copy
bar = std::make_shared<int>(); // move
std::unique_ptr<int> unique(new int());
foo = std::move(unique); // move from unique_ptr
std::cout << "*foo: " << *foo << '\n';
std::cout << "*bar: " << *bar << '\n';

输出:

交换控制权:

std::shared_ptr<int> foo(new int());
std::shared_ptr<int> bar(new int());
foo.swap(bar);
std::cout << "*foo: " << *foo << '\n';
std::cout << "*bar: " << *bar << '\n';

输出:

重置:

std::shared_ptr<int> sp;
sp.reset(new int);
*sp = ;
std::cout << *sp << '\n';
sp.reset(new int);
*sp = ;
std::cout << *sp << '\n';
sp.reset();

输出:

判断是否是唯一的share_ptr

std::shared_ptr<int> foo;
std::shared_ptr<int> bar(new int);
std::cout << "1: " << foo.unique() << '\n'; // false (empty)
foo = bar;
std::cout << "2: " << foo.unique() << '\n'; // false (shared with bar)
bar = nullptr;
std::cout << "3: " << foo.unique() << '\n'; // true

输出:

智能指针share_ptr记录的更多相关文章

  1. 智能指针weak_ptr记录

    智能指针weak_ptr为弱共享指针,实际上是share_ptr的辅助指针,不具备指针的功能.主要是为了协助 shared_ptr 工作,可用来观测资源的使用情况.weak_ptr 只对 shared ...

  2. C++11智能指针 share_ptr,unique_ptr,weak_ptr用法

    0x01  智能指针简介  所谓智能指针(smart pointer)就是智能/自动化的管理指针所指向的动态资源的释放.它是存储指向动态分配(堆)对象指针的类,用于生存期控制,能够确保自动正确的销毁动 ...

  3. 智能指针unique_ptr记录

    unique_ptr 对对象独有管理,无法复制,共享,值传递,可以使用move语义来转移控制权. std::default_delete<int> d; std::unique_ptr&l ...

  4. C++ 标准库智能指针

    整理一下c++中shared_ptr,weak_ptr,unique_ptr三种指针的使用案例和注意事项,让程序资源更加案例,在标准库中,需要包含<memory>,在boost库中, 一. ...

  5. 【校招面试 之 C/C++】第26题 C++ 智能指针(二)之 share_ptr

    1.综述 shared_ptr 是一个标准的共享所有权的智能指针, 允许多个指针指向同一个对象. 定义在 memory 文件中(非memory.h), 命名空间为 std. shared_ptr 是为 ...

  6. 记录智能指针使用shared_ptr使用错误

    shared_ptr为智能指针,今天一次在使用shared_ptr时,错误的将其初始化方式写为shared_ptr<T> test = shared_ptr<T>(),随后导致 ...

  7. 利用模板和C++11特性实现的智能指针-作用同share_ptr

    根据C++11特性实现,基本上实现了同SharePtr同样的功能,有时间继续优化.之前一直以为引用计数是一个静态的int类型,实际上静态值是不可以的.之前项目中总是不太习惯使用智能指针.通过自实现的方 ...

  8. c++11 智能指针 unique_ptr、shared_ptr与weak_ptr

    c++11 智能指针 unique_ptr.shared_ptr与weak_ptr C++11中有unique_ptr.shared_ptr与weak_ptr等智能指针(smart pointer), ...

  9. c++智能指针使用笔记

    1. c++智能指针中,c++的memory文件中,有auto_ptr等各种关于智能指针的东西,shared_ptr,weak_ptr在C++11中已经成为标准. 也看了ogs的智能指针,每次引用起来 ...

随机推荐

  1. PAT甲级 树 相关题_C++题解

    树 目录 <算法笔记>重点摘要 1004 Counting Leaves (30) 1053 Path of Equal Weight (30) 1079 Total Sales of S ...

  2. Python【BeautifulSoup解析和提取网页数据】

    [解析数据] 使用浏览器上网,浏览器会把服务器返回来的HTML源代码翻译为我们能看懂的样子 在爬虫中,也要使用能读懂html的工具,才能提取到想要的数据 [提取数据]是指把我们需要的数据从众多数据中挑 ...

  3. redis 缓存对象、列表

    在spring boot环境下有个StringRedisTemplate对象,默认已经为我们配置好了,只需要自动注入过来就能用,但是使用它只能在Redis中存放字符串.具体操作如下: @RunWith ...

  4. C#xml泛型序列化

    using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Web ...

  5. jacascript 基础数据类型(一)

    前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! 数据类型有 number.boolean.string.object.null.undefined; un ...

  6. 怎样理解数组的空元素empty与undefined的区别

    数组的空元素empty表示空位, 它不是一种数据类型, 而是由于人为修改arr.length 或者写入时多写了逗号造成的. var arr = [1,2,3,4,,,5]; arr.length; a ...

  7. Win10安装PyQt5与Qt Designer

    1.直接在cmd中通过pip安装PyQt5 1 pip install pyqt5 会自动下载PyQt5以及sip并安装,因为PyQt5不再提供Qt Designer等工具,所以需要再安装pyqt5- ...

  8. bootstrap实现Carousel旋转木马(焦点图)

    引入bootstrap相关文件后,在html中写如下代码: <div class="col-lg-9" > <!-- Carousel============== ...

  9. 你不知道的javascript(上卷)读后感(二)

    this词法 熟悉ES6语法的开发者,箭头函数在涉及this绑定时的行为和普通函数的行为完全不一致.跟普通this绑定规则不一样,它使用了当前的词法作用域覆盖了this本来的值. 误解 this理解成 ...

  10. vue处理换行符

    1.处理换行符 <tr class="unread" v-for="(item,index) in DataList" :key="index& ...