C/C++ Arena

Step 2 of 5

Unique means unique

A unique_ptr can't be copied (two owners would both delete). It can be moved, which transfers ownership and leaves the source empty (null):

auto a = std::make_unique<int>(5);
// auto b = a;               // compile error: copying is deleted
auto b = std::move(a);       // ok: b owns it, a is now nullptr

Functions that take ownership accept std::unique_ptr<T> by value; callers must std::move into them.

Your turn: complete Inventory::add, which takes ownership of an item and stores it in the vector. Then count() and total_value().

Previous: unique_ptr Next: Returning ownership