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

猜你喜欢

转载自www.cnblogs.com/sarah-zhang/p/12219851.html
今日推荐