Unique Pointer
They automatically delete the pointed-to object when it's no longer needed, i.e. it goes out of scope.
Key points about std::unique_ptr
:
It's 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.It cannot be copied, only moved, ensuring single ownership.
Use std::make_unique to create a
unique_ptr
(C++14 onwards).Automatically calls delete on the managed object when going out of scope.
Move to contained value (pointer) to another
unique_ptr
usingstd::move(ptr1)
Example:
Last updated