What is a smart pointer?

What is a smart pointer?

- Smart pointers are objects which store pointers to dynamically allocated (heap) objects.
- They are like built-in C++ pointers. However, they automatically delete the object pointed to at the appropriate time.
- They are useful as they ensure proper destruction of dynamically allocated objects (Exceptions).
- They can also be used to keep track of dynamically allocated objects shared by multiple owners.
- They appear as owning the object pointed to and are responsible for deletion of the object when it is no longer needed.
- The smart pointer library provides five smart pointer class templates :

scoped_ptr :
Simple sole ownership of single objects. Noncopyable.
scoped_array :
Simple sole ownership of arrays. Noncopyable.
shared_ptr :
Object ownership shared among multiple pointers
shared_array :
Array ownership shared among multiple pointers.
weak_ptr :
Non-owning observers of an object owned by shared_ptr.
intrusive_ptr :
Shared ownership of objects with an embedded reference count.

- These templates are designed to complement the std::auto_ptr template.
Difference between an inspector and a mutator - C++
Difference between an inspector and a mutator - The get() functions are usually refered to as inspectors as that just retrieve the data values from the source....
What is pointer? - C++
What is pointer? - A pointer is a variable that holds a memory address....
Pointer to constant vs. pointer constant - C++
Pointer to constant vs. pointer constant - Pointer to constant points to a value that does not change and is declared....
Post your comment