c++, std::shared ptr

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

 


posted @ 2020-01-20 23:42  心怀阳光  阅读(140)  评论(0编辑  收藏  举报