This will mainly introduce smart pointers shared_ptr and unique_ptr, and simply implement a smart pointer based on reference counting.
Since c++11, the C + + standard provides two types of smart pointers:
1. Class shared_ptr implements the concept of shared-ownership (GKFX ownership). Multiple smart pointers can point to the same object, and the object and its related resources are released when the last reference (reference) is destroyed. To perform the above work in a complex structure, the standard library provides auxiliary classes such as weak_ptr, Bad_weak_ptr, and Enable_shared_from_this.
2. Class Unique_ptr implements an exclusive owning (exclusive ownership) or strictly owned (strict ownership) concept, guaranteeing that only one smart pointer can point to the object at the same time. It is particularly useful for avoiding resource leaks (resourece leak)-such as "forgetting to call delete after an exception has been created with new."
Note: Class auto_ptr in c++98 is no longer recommended for use in c++11.
shared_ptr
Unique_ptr
Simple implementation of smart pointers
C + + Smart pointers and their simple implementation