The C++ library provides implementations of the following types of smart Pointers:
auto_ptr
unique_ptr
shared_ptr
weak_ptr
They are declared in an in-memory header file.
Article source: geeksforGeeks
auto_ptr
Starting from C ++ 11, this type of template is not recommended. Unique_ptr is a new feature with similar functionality but improved security.
Auto_ptr is a smart pointer that manages objects obtained through new expressions and deletes the object when the auto_ptr itself is destroyed.
When you use the auto_ptr class to describe objects, it stores Pointers to a single allocated object to ensure that when it goes out of scope, the object it points to must be destroyed automatically. It is based on the proprietary ownership model, which means that two Pointers of the same type cannot point to the same resource at the same time. As shown in the following program, copying or allocation of Pointers changes ownership, meaning that the source pointer must give ownership to the target pointer.
The copy constructor and the assignment operator for auto_ptr do not actually copy the stored pointer, but transfer it, leaving the first auto_ptr empty. This is one way to achieve strict ownership, so that only one auto_ptr object can own the pointer at any given time, that is, auto_ptr should not be used where semantics need to be copied.
Why not use auto_ptr?
It takes ownership of Pointers in such a way that neither pointer should contain the same object. Assignment transfers ownership and resets the rvalue auto pointer to null. Therefore, because the above cannot be copied, you cannot use them in an STL container.
unique_ptr
STD :: unique_ptr was developed in C ++ 11, replacing STD :: auto_ptr.
Unique_ptr is a new facility with similar functionality, but with improved security (no forged copy assignment), increased functionality (remover), and support for arrays. It is the container for the original pointer. It explicitly prevents the copying of the pointer it copies, as would happen with a normal assignment, that is, it allows only one owner of the underlying pointer.
Therefore, when unique_PTR is used, there can be at most one unique_PTR for any one resource, and when the unique_PTR is destroyed, the resource is automatically declared. Also, since there can only be one unique_ptr for any resource, any attempt to copy unique_ptr results in a compile-time error.
Unique_ptr ptr1 (新A); Unique_ptr ptr2 = ptr1; unique_ptr ptr2 = ptr1;
However, you can move the unique_ptr using the new move semantics, that is, transferring ownership of the contained pointer to another Unique_PTR using the STD :: Move () function.
Unique_ptr ptr2 = move (ptr1);
Therefore, it is best to use unique_ptr when we want to point to a single pointer to an object and destroying that single pointer will reclaim that object.
The following code returns a resource that will be cleared if we do not explicitly capture the return value. If we do, we have exclusive ownership of the resource. In this way, we can think of unique_PTR as a safer and better alternative to auto_PTR.
When to use unique_ptr?
Use unique_ptr when you want to have sole ownership (exclusive) of a resource. Only one unique_ptr can point to a resource. Because a single resource can have a Unique_Ptr, it is not possible to copy a Unique_PTR to another resource.
shared_ptr
Shared_ptr is the container for the original pointer. It is a reference-counting ownership model, that is, it collaborates with all copies of Shared_Ptr to maintain a reference count of the Pointers it contains. Thus, the counter is increased each time a new pointer points to a resource and decreased when the object’s destructor is called.
Reference counting: This is a technique for storing the number of references, Pointers, or handles to resources (such as objects, chunks of memory, disk space, or other resources).
The object referenced by the contained original pointer is not destroyed until the reference count is greater than zero (that is, until all copies of shared_ptr have been deleted).
Therefore, when we want to assign a raw pointer to multiple owners, we should use shared_ptr.
When to use shared_ptr?
To share resource ownership, use shared_ptr. Many shareD_ptr points to a resource. Shared_ptr maintains a reference count for this proposal. When all shareD_ptr points to a resource out of scope, the resource is destroyed.
weak_ptr
Create weak_ptr as a copy of shared_Ptr. It provides access to objects owned by one or more shared_Ptr instances that do not participate in reference counting. The presence or destruction of weak_ptr has no effect on shared_Ptr or its other copies. In some cases, you need to break the circular reference between shared_ptr instances.
Cyclic dependencies (shared_ptr issue) :
Let’s consider A scenario where we have two classes A and B, both of which have Pointers to other classes. So it’s always like A points to B, B points to A. Therefore, use_count never reaches zero and is never deleted.
That’s why we use weak Pointers (weak_ptr), because they have no reference count. Therefore, classes that declare Weak_Ptr do not have its strongpoints, that is, ownership is not shared, but they do have access to these objects.
Therefore, using weak_ptr can prevent this problem in cases where use_count will never be zero due to shared_ptr due to cyclic dependencies. This is solved by declaring A_ptr as Weak_ptr. Therefore, class A does not own it, only accesses it, we also need to check the validity of the object. Because it might be out of range. Usually, this is a design problem.
When is weak_Ptr used?
When you do want to reference objects from more than one location – for references that can be ignored and unassigned (so when you try to unreference them, they just indicate that the object has disappeared).
Every day to learn a little knowledge, I hope to help you ~
In addition, if you want to improve your programming ability, learn C language C++ programming! Pass on the curve, one step faster! The author here may be able to help you ~
Share (source code, project actual combat video, project notes, basic introduction tutorial)
Welcome to change careers and learn programming partners, use more information to learn and grow faster than their own thinking oh!