C++ doesn't have reflection like C# or Java does. This is not a mistake. Reflection has been considered more than once, and intentionally left out of the language.
HOWEVER, you can get something close to your design. You will have to do a lot of work yourself. You will have to, essentially, implement reflection in a language that intentionally doesn't provide it.
Your current design is close. If I were to implement reflection the way you appear to be going I would do:
class Reflection_registry {
std::map<std::string, Handler*> registry_;
public:
void registerClass(const std::string& name, Handler* handler) {
registry_[name] = handler;
}
Handler* getHandlerForClass(std::string& name) {
std::map<std::string, Handler*>::iterator itor = registry_.find(name);
return itor == registry_.end() ? NULL : itor->second;
}
}
The thing is that you will be responsible for coming up with the relevant names, by hand, and keeping them up to date. As others have suggested, you can use typeid
and std::type_info
s to generate the names -- that's pretty much the kind of thing std::type_info
was designed for.