// Object.h classObject { protected: using String = std::string_view; using Creater = std::function<std::shared_ptr<Object>()>; using Container = std::unordered_map<String, Creater>;
public: virtualvoidInit()= 0; // 一个功能函数示例
staticstd::shared_ptr<Object> Create(const String& type){ auto pos = _objs.find(type); if (pos == _objs.end()) { std::cout << "String \"" << type << "\" has not registered, you may need to use \"IMPL_OBJECT_CREATION(" << type << ")\" in .cpp file\n"; returnnullptr; } return _objs[type](); } protected: staticvoid _regist(const String& type, const Creater& creater) { if (_objs.count(type)) { std::cout << "Multiple registration of type: " << type << std::endl; } _objs[type] = creater; } private: static Container _objs; };