std::weak_ptr Member Functions
The following are some member functions associated with std::weak_ptr to provide different functionalities.
S.No. | Functions | Description |
---|---|---|
1 |
reset() | Clear the weak_ptr. |
2 |
swap | Specialization of std:swap(). It swaps the objects managed by weak_ptr. |
3 |
expired() | Check if the resource weak_ptr pointing to exists or not. |
4 |
lock() | If the resource pointed by weak_ptr exists, this function returns a shared_ptr with ownership of that resource. If the resource does not exist, it returns default constructed shared_ptr. |
5 |
use_count() | Tells about how many shared_ptr owns the resource. |
weak_ptr in C++
The weak_ptr is one of the smart pointers that provide the capability of a pointer with some reduced risks as compared to the raw pointer. The weak_ptr, just like shared_ptr has the capability to point to the resource owned by another shared_ptr but without owning it. In other words, they are able to create a non-owning reference to the object managed by shared_ptr.
Contact Us