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

std::shared_ptr foo;        

std::shared_ptr 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)