我的主程序读取配置文件,配置文件告诉它运行哪些函数.这些函数在一个单独的文件中,当我创建一个新函数时,我必须在主程序中添加函数调用(因此当配置文件指示它时可以引发它)
我的问题是,有什么方法可以让主程序单独使用,当我添加一个新函数时,它可以通过某种数组调用.
例如(忍受我,我不确定你能做到这一点).
我有一个数组(或枚举),
char functions [3] = ["hello()","run()","find()"];
Run Code Online (Sandbox Code Playgroud)
当我读取配置文件并且它说运行hello()时,我可以使用数组运行它(我可以找到数组中是否存在测试)
我也可以轻松地向阵列添加新功能.
注意:我知道它不能用数组完成,所以只是一个例子
我认为这样的事情.
#include <functional>
#include <map>
#include <iostream>
#include <string>
void hello()
{
std::cout << "Hello" << std::endl;
}
void what()
{
std::cout << "What" << std::endl;
}
int main()
{
std::map<std::string, std::function<void()>> functions =
{
std::make_pair("hello", hello),
std::make_pair("what", what)
};
functions["hello"]();
}
Run Code Online (Sandbox Code Playgroud)
http://liveworkspace.org/code/49685630531cd6284de6eed9b10e0870
| 归档时间: |
|
| 查看次数: |
2022 次 |
| 最近记录: |