Step 2 of 6
A factory registry
A factory creates objects by name (from a config file, a network message, a command line), returning them through a base-class pointer. Instead of a giant if/else chain, large codebases use a registry: a map from name to a creator function. New types register themselves without anyone editing the factory.
using Creator = std::function<std::unique_ptr<Weapon>()>;
std::map<std::string, Creator> creators_;
Returning std::unique_ptr makes ownership obvious: the caller owns the new object.
Your turn: implement Factory::add (returns false if the name is already registered), Factory::create (returns nullptr for unknown names) and Factory::names (sorted).
Previous: Strategies as closures Next: Observers with weak_ptr