调用函数从字符串函数的使用C ++名称 [英] Calling a Function From a String With the Function’s Name in C++

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

问题描述

我怎么能调用C ++函数从字符串

How can I call a C++ function from a string?

而不是这样做的,直接从字符串调用方法:

Instead of doing this, call the method straight from string:

void callfunction(const char* callthis, int []params)
{
  if (callthis == "callA")
  {
    callA();
  }
  else if (callthis == "callB")
  {
    callB(params[0], params[1]);
  }
  else if (callthis == "callC")
  {
    callC(params[0]);
  }
}

在C#中,我们会使用的typeof(),然后获得方法的信息,并从那里叫...什么我们能在C ++中使用?

In C# we'd use typeof() and then get the method info and call from there... anything we can use in C++?

推荐答案

创建由字符串和函数指针的std ::地图。创建所有这一切你将要调用的函数映射。

Create a std::map made of strings and function pointers. Create the map with all of the functions that you will want to call.

有其他的方法来做到这一点,包括符号表,动态加载程序,但这些方式都无法移植或友好。

There are other ways to do it, involving symbol tables and dynamic loaders but those ways are not portable or friendly.

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

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