小编Ral*_*ger的帖子

在地图中存储具有不同签名的函数

我想创建一个map具有 string关键和通用的方法,value在C++中,但我不知道这甚至有可能.我想做那样的事情:

void foo(int x, int y)
{
   //do something
}

void bar(std::string x, int y, int z)
{
   //do something
} 

void main()
{
   std::map<std::string, "Any Method"> map;

   map["foo"] = &foo;      //store the methods in the map
   map["bar"] = &bar;

   map["foo"](1, 2);       //call them with parameters I get at runtime
   map["bar"]("Hello", 1, 2);
}
Run Code Online (Sandbox Code Playgroud)

那可能吗?如果是的话,我怎么能意识到这一点?

c++

15
推荐指数
2
解决办法
2600
查看次数

标签 统计

c++ ×1