C ++中的动态源代码 [英] Dynamic source code in C++
问题描述
如何处理C ++中的动态源代码?可以使用像eval(foo)这样的东西吗?
根据用户的选择,我有一些需要调用的功能:
void function1();
void function2();
...
void functionN();
int main(int argv,char * argv [])
{
char * myString = new char [100];
...
myString =1//用户输入
cout<<<的eval( 功能 + myString的)();
}
通常如何做?
UPD :根据slacy和clinisbut的答案,我想我需要做一个功能注册表。我想这应该是一个指向函数的数组。这是一个问题,如何声明一个指向函数的数组?
C ++是一种编译语言,因此,对于您提到的具体示例,您可以创建一个将输入(字符串)映射到输出(函数指针)的函数注册表,这个函数不是等价于eval()。
<然后调用结果函数。
有几个可用的C ++解释器库,尽管性能会非常差,可能会完成你想要的。 Google搜索C ++解释器。我看到 Ch , CINT 和clipp
How to process dynamic source code in C++? Is it possible to use something like eval("foo")?
I have some functions that need to be called depending on user's choice:
void function1 ();
void function2 ();
...
void functionN ();
int main (int argv, char * argv [])
{
char * myString = new char [100];
...
myString = "1" //user input
cout << eval("function"+myString)();
}
How is it usually done?
UPD: Based on slacy's and clinisbut's answers I think I need to make a function registry. I suppose it should be made as an array of pointers to functions. Here's the question, how do I declare an array of pointers to functions?
C++ is a compiled language, and thus, there is no equivalent to "eval()".
For the specific example you mention, you could make a function registry that maps inputs (strings) to outputs (function pointers) and then call the resultant function.
There are several C++ interpreter libraries that are available, although performance will be very poor, they may accomplish what you want. Google search for "C++ interpreter". I've seen results for "Ch", "CINT" and "clipp"
这篇关于C ++中的动态源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!