C ++中的动态源代码 [英] Dynamic source code in C++

查看:159
本文介绍了C ++中的动态源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C ++中处理动态源代码?是否可以使用像eval(foo)?

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)();
     }

通常如何做?

UPD :根据slacy和clinisbut的答案,我想我需要做一个函数注册表。我想它应该作为一个指针数组的函数。这里的问题是,如何声明一个函数指针数组?

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 ++是一种编译语言,因此,

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.

有几个C ++解释库可用,虽然性能会很差,他们可能会完成你想要的。 Google搜索C ++ interpreter。我看到了有关 Ch CINT 和clipp

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆