C++ 在C/C+中自动调用函数+;

C++ 在C/C+中自动调用函数+;,c++,c,C++,C,我的主程序读取配置文件,配置文件告诉它要运行哪些函数。这些函数在一个单独的文件中,当前每当我创建一个新函数时,我必须在主程序中添加函数调用(因此,当配置文件指示时,可以调用该函数) 我的问题是,有没有办法让主程序保持独立,当我添加一个新函数时,它可以通过某种数组调用 例如(请相信我,我不太确定你能做到这一点) 我有一个数组(或枚举) 当我读取配置文件并显示run hello()时,我可以使用数组运行它吗(我可以找到数组中是否存在测试) 此外,我还可以轻松地向数组添加新函数 注意:我知道这不能用数

我的主程序读取配置文件,配置文件告诉它要运行哪些函数。这些函数在一个单独的文件中,当前每当我创建一个新函数时,我必须在主程序中添加函数调用(因此,当配置文件指示时,可以调用该函数)

我的问题是,有没有办法让主程序保持独立,当我添加一个新函数时,它可以通过某种数组调用

例如(请相信我,我不太确定你能做到这一点)

我有一个数组(或枚举)

当我读取配置文件并显示run hello()时,我可以使用数组运行它吗(我可以找到数组中是否存在测试)

此外,我还可以轻松地向数组添加新函数


注意:我知道这不能用数组来完成,所以举个例子,使用函数指针来完成

例如(我不确定语法):

map函数;

然后做
funcs[name]()

我想是这样的

#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"]();
}
#包括
#包括
#包括
#包括
void hello()
{

std::cout签出(也称为“functors”)。您可以有条件地调用对您选择的各种函数之一的引用,而不是调用特定函数。教程提供了关于该主题的详细介绍。

从主函数中公开一个可以注册新元组的函数
{function_name,function_pointer}
在地图中(如其他答案所建议)

通常:

// main.h
typedef void (*my_function)(void *);
int register_function(const std::string &name, my_function function);

// main.c
int register_function(const std::string &name, my_function function)
{
  static std::map<std::string, my_function> s_functions;
  s_functions[name] = function;
  return 0;
}

int main()
{
  for( std::map<std::string, my_function>::const_iterator i=s_functions.begin();
       i != s_functions.end();
       ++i)
  {
    if( i->second )
    {
      // excecute each function that was registered
      (my_function)(i->second)(NULL);
    }
  }

  // another possibility: execute only the one you know the name of:
  std::string function_name = "hello";
  std::map<std::string, my_function>::const_iterator found = s_functions.find(function_name);
  if( found != s_functions.end() )
  {
    // found the function we have to run, do it
    (my_function)(found->second)(NULL);
  }
}

这意味着,每次使用此类语句添加新的源文件时,它都会自动添加到可执行的函数列表中。

是否可以将其设置为void*函数并接收数据,因此void*hello(void*data)@Rave是的,你所有的函数都应该有相同的原型。std::function。我所有的函数都接受void*并返回void,因为我把它们用作线程函数。我尝试过使用std::function,我得到了errors@Rave你的编译器是什么?它是C++11标准。你也可以使用boost::函数,当然还有键字符串映射和指向函数的值指针。我正在使用g++,我将尝试boostThank,这似乎是现在对我来说最好的实现。我得到了我的_函数,没有在main.c.static std::map s_函数中声明;我包括了头,我正在使用g++是的,这是我的
main.h
文件中的一个输入错误。应该是:
typedef void(*my_函数)(void*);
(我错放了第一个
*
)你就快到了。void(*)就是答案。
#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"]();
}
// main.h
typedef void (*my_function)(void *);
int register_function(const std::string &name, my_function function);

// main.c
int register_function(const std::string &name, my_function function)
{
  static std::map<std::string, my_function> s_functions;
  s_functions[name] = function;
  return 0;
}

int main()
{
  for( std::map<std::string, my_function>::const_iterator i=s_functions.begin();
       i != s_functions.end();
       ++i)
  {
    if( i->second )
    {
      // excecute each function that was registered
      (my_function)(i->second)(NULL);
    }
  }

  // another possibility: execute only the one you know the name of:
  std::string function_name = "hello";
  std::map<std::string, my_function>::const_iterator found = s_functions.find(function_name);
  if( found != s_functions.end() )
  {
    // found the function we have to run, do it
    (my_function)(found->second)(NULL);
  }
}
// hello.c
#include "main.h"

void my_hello_function(void *)
{
  // your code
}

static int s_hello = register_function("hello", &my_hello);