I wish to have a map from classes to instances of each class. The goal is to have a container of components for a component-based game engine where an object can have at most one of each component type.
In Java, I could just use class objects as the key. How can I do something similar in C++? My initial research suggests something like using typeid(component_instance).name() as the key. Is there a better way to do this?
Unlike in more dynamic languages like Python or Java, classes in C++ are not objects themselves. At runtime they merely do not exist (as for a programmers point of view).
Your
typeidapproach is not that bad, but for performance issues I’d use a hash or a numeric ID (like an integer defined as static in your class) instead of the string.