小编Rin*_*ro.的帖子

如何将方法存储为地图容器中的函数指针?

我希望能够根据从文件中读取的数据调用函数.因此,对于每种项目类型,我想调用所需的读者方法.我编写了这段代码,但它没有编译我想在地图中添加函数指针的地方.怎么了?

#include <vector>
#include <map>
#include <iostream>
class reader
{

  std::map< std::string, void(*)()> functionCallMap; // function pointer
  void readA(){ std::cout << "reading A\n";};
  void readB(){ std::cout << "reading B\n";};;
public:
  reader()
  {
    *functionCallMap["A"] = &reader::readA;*
    *functionCallMap["B"] = &reader::readB;*
  }

  void read()
  {
   auto (*f) = functionCallMap["A"];
   (*f)();
  }



};
Run Code Online (Sandbox Code Playgroud)

我在Constructor填写地图.

c++ pointers function

17
推荐指数
3
解决办法
1707
查看次数

标签 统计

c++ ×1

function ×1

pointers ×1