0Pricing
C++ Academy · Lesson

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 args

All lessons in this course

  1. Why Smart Pointers Replace Raw new delete
  2. unique_ptr Exclusive Ownership
  3. shared_ptr and weak_ptr Shared Ownership
  4. Custom Deleters and Make Functions
← Back to C++ Academy