功能图c ++ [英] Map Of functions c++
本文介绍了功能图c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经绘制了功能图.所有这些函数都是无效的,并且接收单个字符串参数.
I have made a map of functions. all these functions are void and receive single string parameter.
代码:
void f1(string params){...}
void f2(string params){...}
void f3(string params){...}
map<string , void*> funcMap;
funcMap["f1"] =(void*)&f1;
funcMap["f2"] =(void*)&f2;
funcMap["f3"] =(void*)&f3;
我该如何调用函数? 我尝试了下一个代码,但是id不起作用:
how do i call a function? I tried the next code, but id doesn't work:
void (*func)(string) = &funcMap[commandType];
func(commandParam);
我收到此错误消息:
Server.cpp:160:46: error: cannot convert ‘void**’ to ‘void (*)(std::string) {aka void (*)(std::basic_string<char>)}’ in initialization
推荐答案
typedef void (*pfunc)(string);
map<string, pfunc> funcMap;
funcMap["f1"] = f1; //and so forth
然后致电:
pfunc f = funcMap[commandType];
(*f)(commandParam);
通常,为什么要扔掉类型安全性?如果它是函数指针的映射,则将其声明为一个.
In general, why throw away type safety? If it's a map of function pointers, declare it to be one.
这篇关于功能图c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文