Member Methods of shared_ptr

Following are some members associated with shared_ptr:

Method Description
reset() Resets the std::shared_ptr to empty, releasing ownership of the managed object.
use_count() Returns the current reference count, indicating how many std::shared_ptr instances share ownership.
unique() Check if there is only one std::shared_ptr owning the object (reference count is 1).
get() Returns a raw pointer to the managed object. Be cautious when using this method.
swap(shr_ptr2) swaps the contents (ownership) of two std::shared_ptr instances.

shared_ptr in C++

std::shared_ptr is one of the smart pointers introduced in C++11. Unlike a simple pointer, it has an associated control block that keeps track of the reference count for the managed object. This reference count is shared among all the copies of the shared_ptr instances pointing to the same object, ensuring proper memory management and deletion.

Shared Pointer in C++

Similar Reads

Syntax of std::shared_ptr

The shared_ptr of type T can be declared as:...

Member Methods of shared_ptr

Following are some members associated with shared_ptr:...

Examples of std::shared_ptr

Example 1:...