Programming‎ > ‎Language‎ > ‎C++‎ > ‎Random‎ > ‎

Smart pointer

If we make them smarter they will become self-aware

Smart pointers are objects that store pointers to dynamically allocated objects. The purpose of them is to manage to lifetime of the object being pointed to. The smart pointer owns the object it points to and to access the object you must use the smart pointer. When the smart pointer goes out of scope it checks that no one us still using the object it points to and if so it deletes it.

std::unique_ptr - C++11 - For exclusive-ownership of a resource
std::shared_ptr - C++11 - For shared-ownership of a resource
std::weak_ptr - C++11
A weak_ptr holds a reference to an object managed by a shared_ptr, but does not contribute to the reference count. To access the object one use the lock() function on the weak_ptr to create a shared_ptr.

std::auto_ptr - C++98
This type of pointer is deprecated since C++11 and it has been replaced by std::unique_ptr.