以字符串形式给出的名称调用函数 [英] Call function by name given as a string

查看:42
本文介绍了以字符串形式给出的名称调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在c ++中是否有可能调用另一个函数中以字符串arg给出名称的函数.例如:

Is there possibility in c++ to call function which name was given as a string arg in another function. for example:

void func1()
{
// do something
}

void func2()
{
// do something
}

int main()
{
    call("func1");
    call("func2");
}

推荐答案

您可以创建 std :: map "noreferrer"> std :: function C ++ 11

You can create a std::map of std::functionC++11

  std::map<std::string, std::function<void()> > call;

  call["func1"] = func1 ;
  call["func2"] = func2 ;

然后

  call["func1"]( ) ;

这篇关于以字符串形式给出的名称调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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