Smart PointersA smart pointer is a class (template) that is implemented based on the RAII mechanism, with pointer behavior (overloading the operator* and operator-> operators) to "intelligently" destroy the objects it refers to. C++11 has unique_ptr, shared_ptr and weak_ptr and other intelligent pointers, can manage dynamic resources.
A smart pointer is a class, not a pointer
C++11 The Boost library smart pointer into the standard
unique_ptrUnique_ptr replaces the original auto_ptr, which points to the uniqueness of the object, that is, only unique_ptr point to a given object at the same time (and Auto_ptr is different from the copy semantics, replaced by the mobile semantics) unique_ The PTR object lifecycle is consistent with its scope, from creation until it leaves the scope
Unique_ptr the pointer and the object to which it is bound: within the life cycle of the smart pointer, you can change its bound object, you can reassign it by reset (), you can release it by releasing it, and transfer its ownership by moving semantics
<span style= "White-space:pre" > </span>std::unique_ptr<int> u_i;//Create empty smart pointer
u_i.reset (NEW Int (1));//Bind dynamic Object
unique_ptr<int> u_i2 (new int (2));
int *p_i = U_i2.release ()//free ownership This time it is necessary to manually release the dynamically allocated memory
delete p_i; p_i = nullptr;
Unique_ptr<string> u_s (New string ("1213"))//bind
//unique_ptr<string> u_s1 = u_s;//Compile error, assignment function is private, Cannot transfer ownership by assignment function
unique_ptr<string> u_s2 = Std::move (u_s);//Transfer ownership
U_S2 = nullptr
through move semantics; Vector<unique_ptr<int>> vi;//can be used as a container element
Vi.push_back (std::move (new int (1))) ;
Unique_ptr <int[]> Vi_array (New Int[3]{1, 2, 3})//Can be destroyed by a dynamic array for the base type call delete[]
vi_array[0] = 2;//overload [ ]
//Custom resource deletion operation
//uinque_ptr default release resource operation is delete/delete[]
unique_ptr<com, Decltype (safereleasecom) * > u_com (New COM (), safereleasecom);
shared_ptrSHARED_PTR is a reference count pointer that destroys an object without any other shared_ptr pointing to an object that was originally pointed to
shared_ptr and weak_ptr Intelligent pointers are both the application of C + + RAII, which can be used in dynamic resource management
SHARED_PTR is implemented based on the reference count model, where multiple shared_ptr can point to the same dynamic object and maintain a shared reference counter that records the number of shared_ptr instances referencing the same object. When the last shared_ptr that points to a dynamic object is destroyed, it automatically destroys the object it refers to (through the delete operator).
The default capability of shared_ptr is to manage dynamic memory, but to support custom deleter to implement personalized resource release actions.
WEAK_PTR is used to solve the "reference count" model circular dependency problem, weak_ptr point to an object, does not increase or decrease the reference counter of the object
shared_ptr creation, copying, binding object changes (reset), shared_ptr destruction (manually assigned to nullptr or out of scope), deleter, and so on, and uique_ptr are different shared_ptr assignment copies can be used , at which point the reference count plus a
weak_ptrWEAK_PTR is used in conjunction with shared_ptr, and does not affect the lifecycle of dynamic objects, whether their existence does not affect the object's reference counter.
WEAK_PTR does not overload the operator-> and operator * operators, so objects cannot be used directly through weak_ptr.
Provides a expired () and lock () member function that is used to determine whether the object that weak_ptr points to has been destroyed, and which returns the SHARED_PTR smart pointer to the object that it refers to (returning an "empty" shared_ptr when the object is destroyed).
A loop-referenced scene: A circular reference between a parent node and a child node in a binary tree, a circular reference between a container and an element, etc.
shared_ptr Use scenes"Shared data" between objects, object creation and destruction "detach"
The dynamic object in the container, using shared_ptr packaging, is more suitable than the UNIQUE_PTR
When managing dynamic arrays, you need to develop deleter to destroy memory using the delete[] operator because SHARED_PTR does not have a special version of the array (Unique_ptr has a special version for the array)