#include #include using std::string; using std::map; class Object { public: virtual string ToString() const = 0; virtual ~Object() {} }; class smth : public Object { public: explicit smth(string str) { strr = str; } string ToString() const override { return strr; } private: string strr; }; Object* new_smth(string id) { return new smth(id); } class Factory { public: Factory() { reg["apple!"] = []() -> Object* { return new smth("apple!"); }; reg["list"] = []() -> Object* { return new smth("list"); }; reg["yet another identifier"] = []() -> Object* { return new smth("yet another identifier"); }; } ~Factory() = default; Object* Create(const string& class_id) { return reg[class_id](); } void Register(string class_id, Object* (*new_smth)()) { reg[class_id] = new_smth; } private: map reg; };