码迷,mamicode.com
首页 > 编程语言 > 详细

c++, std::shared ptr

时间:2020-01-21 00:40:14      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:cti   des   sam   ptr   unique   func   reset   memory   point   

0. 

1. std::shared_ptr is a smart pointer that retains shared ownership of an object through a pointer. Several shared_ptr objects may own the same object. The object is destroyed and its memory deallocated when either of the following happens: (1) the last remaining shared_ptr owning the object is destroyed. (2) the ownership is transfed via operator= or reset()

2. Some examples, showing how to use member functions

#include <memory>

std::shared_ptr<int> foo;        

std::shared_ptr<int> bar(new int);

std::cout << foo.unique(); // false;

foo = bar; // assignment 

std::cout << foo.unique(); // false, shared with bar

std::cout << foo.use_count(); // 2

bar = nullptr;

std::cout << foo.unique(); // true

 

c++, std::shared ptr

标签:cti   des   sam   ptr   unique   func   reset   memory   point   

原文地址:https://www.cnblogs.com/sarah-zhang/p/12219851.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!