May 2017
Intermediate to advanced
590 pages
17h 18m
English
In the preceding implementation, the map is a local static to the virtual function, but it can also be a member of the class or even a global. The following implementation has the map defined as a static member of the class, and the objects are not created based on the format name, but on the type information, as returned by the typeid operator:
struct IImageFactoryByType { virtual std::shared_ptr<Image> Create(std::type_info const & type) = 0; }; struct ImageFactoryByType : public IImageFactoryByType { virtual std::shared_ptr<Image> Create(std::type_info const & type) override { auto it = mapping.find(&type); if (it != mapping.end()) return it->second(); return nullptr; } private: static std::map< std::type_info const ...Read now
Unlock full access