What is a weak pointer C++?
Emily Wilson
Updated on May 18, 2026
.
Simply so, what is a unique pointer C++?
(since C++11) std::unique_ptr is a smart pointer that owns and manages another object through a pointer and disposes of that object when the unique_ptr goes out of scope.
Furthermore, what are the smart pointers in C++? Smart pointers are objects which store pointers to dynamically allocated (heap) objects. They behave much like built-in C++ pointers except that they automatically delete the object pointed to at the appropriate time.
Just so, why do we need weak pointers?
For recently accessed objects, you want to keep them in memory, so you hold a strong pointer to them. This is exactly what a weak pointer does -- it allows you to locate an object if it's still around, but doesn't keep it around if nothing else needs it.
Should I use smart pointers C++?
A smart pointer is a class that wraps a 'raw' (or 'bare') C++ pointer, to manage the lifetime of the object being pointed to. There is no single smart pointer type, but all of them try to abstract a raw pointer in a practical way. Smart pointers should be preferred over raw pointers.
Related Question Answers