我的问题不是我不能映射到函数指针,而是更多的方式。std ::映射函数指针
用我目前的设置,我可以通过字符串实例化类。 现在,我试图从类类型中获取字符串。
我提出的方法:
class A {};
template <typename T> T* create(void) { return new T; }
static std::map<std::string,A*(*)(void)> str_to_class;
static std::map<A*(*)(void),std::string> class_to_str;
template <typename T> void Bind(std::string identity) {
// T must inherit from A.
str_to_class[identity]=&create<T>;
class_to_str[&create<T>]=identity;
}
A* MakeFromString(std::string identity) {
return str_to_class[identity](); // Compiles fine.
}
template <typename T> std::string GetTypeString(void) {
return class_to_str[&create<T>]; // Error!
}
int main(int,char**) {
Bind<A>("A");
A* new_entity=CreateFromString("A");
}
Error: C2679: binary '[' : no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)
我知道我可以使用的dynamic_cast <>检查实体类型,但这需要为每个将被用于类编写代码。
哪里是'模板创建()'声明/定义? –
哎呀,有“构造”和“创造”混合起来。固定。 – object
尝试为该问题创建一个[SSCCE](http://sscce.org) - 您提供的代码不完整,并且当您填写不完整的部分时,它会编译/正常工作... –