Step 7 of 8
The rule of zero
Everything in the last few steps (destructor, deep copy, copy-and-swap) is only needed because the class held a raw owning pointer. Replace it with a member that already manages itself, like std::vector, std::string or std::unique_ptr, and you write none of them. The compiler-generated versions just do the right thing, member by member.
That's the rule of zero, and it's how most modern C++ classes are written. Writing your own destructor or copy operations is the exception, reserved for low-level resource wrappers.
class Loadout {
std::vector<std::string> items_; // copies, moves and frees itself
public:
void add(std::string item) { items_.push_back(std::move(item)); }
};
Your turn: write class Inventory with the rule of zero: void add(const std::string& item), int count() const, bool has(const std::string& item) const, void clear(). No new, no delete, no destructor.
Previous: Copy assignment with copy-and-swap Next: Challenge: a scope guard