是否有C / C ++等效的eval(“function(arg1,arg2)”)? [英] Is there C/C++ equivalent of eval("function(arg1, arg2)")?
本文介绍了是否有C / C ++等效的eval(“function(arg1,arg2)”)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
它需要一种方法来调用函数,其名称存储在类似于eval的字符串中。你能帮我吗?
it need a way to call function whose name is stored in a string similar to eval. Can you help?
推荐答案
C ++没有反射,所以你必须攻击它, e。:
C++ doesn't have reflection so you must hack it, i. e.:
#include <iostream>
#include <map>
#include <string>
#include <functional>
void foo() { std::cout << "foo()"; }
void boo() { std::cout << "boo()"; }
void too() { std::cout << "too()"; }
void goo() { std::cout << "goo()"; }
int main() {
std::map<std::string, std::function<void()>> functions;
functions["foo"] = foo;
functions["boo"] = boo;
functions["too"] = too;
functions["goo"] = goo;
std::string func;
std::cin >> func;
if (functions.find(func) != functions.end()) {
functions[func]();
}
return 0;
}
这篇关于是否有C / C ++等效的eval(“function(arg1,arg2)”)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文