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.
Prerequisites: Pointers in C++, Smart Pointers in C++.
Contact Us