If a pointer variable exists in the class, the memory of the pointer variable is dynamically allocated. There are two main problems.
(1) In terms of assignment, replication, and so on, we only talk about the mutual replication of pointer values, but the pointer pointing to the object is public. If an object is destructed, will delete the public pointer to the object, the other object will appear a suspension pointer.
Solution:
1) Deep replication: creates an object and assigns the same value each time.
2) reference count. The public object is deleted only when the object is 0.
(2) When a class object is dynamically created, this class contains pointer variables for dynamic memory allocation. If the code exception occurs before the delete operation, the dynamically allocated memory of this object will be leaked.
Therefore, a class encapsulation occurs. The RAII resource allocation is initialization, And the auto_ptr class is a specific application.
This article is from "Li Haichuan" blog, please be sure to keep this source http://lihaichuan.blog.51cto.com/498079/1290444