C++ templated class implementation of the multiton pattern
问题 I implemented the multiton pattern using a templated class in C++. #ifndef MULTITON_H #define MULTITON_H #include <map> template <typename Key, typename T> class Multiton { public: static void destroy() { for (typename std::map<Key, T*>::iterator it = instances.begin(); it != instances.end(); ++it) { delete (*it).second; } } static T& getRef(const Key& key) { typename std::map<Key, T*>::iterator it = instances.find(key); if (it != instances.end()) { return *(T*)(it->second); } T* instance =