C++ Pointers & Functions Question:
Download Questions PDF

What is 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.

Download C++ Pointers & Functions Interview Questions And Answers PDF

Previous QuestionNext Question
What is pointer to member?What is Pointer to constant?