unique_ptr Exclusive Ownership
Express single-owner semantics with unique_ptr and move-only types.
The Single-Owner Smart Pointer
std::unique_ptr<T> owns one heap object. When the unique_ptr is destroyed, it deletes the object. Cannot be copied — only moved.
Creating a unique_ptr
Use std::make_unique (C++14) — exception safe and concise.
#include <memory>
auto w = std::make_unique<Widget>(); // default-constructed
auto p = std::make_unique<Point>(3, 4); // forwarded argsAll lessons in this course
- Why Smart Pointers Replace Raw new delete
- unique_ptr Exclusive Ownership
- shared_ptr and weak_ptr Shared Ownership
- Custom Deleters and Make Functions