C/C++ Arena

Step 3 of 5

Returning ownership

Returning a unique_ptr from a function hands ownership to the caller. It's the modern replacement for "returns a pointer you must delete":

std::unique_ptr<Enemy> spawn(int level) {
    return std::make_unique<Enemy>(level * 100);
}

It can also be empty: return nullptr to say "nothing".

Your turn: write std::unique_ptr<std::string> make_callout(const std::string& site) that returns a new string "bomb " + site for sites A and B, and nullptr for anything else.

Previous: Unique means unique Next: shared_ptr