0

I would like to know how to fill this type of map and mainly the way to access to the function pointer.

The map :

enum enum1
{
   val11,
   val12,
   val13
};

enum enum2
{
   val21,
   val22,
   val23
};

typedef void(MyClass::*funcPtr)();

std::map<std::pair<enum1, enum2>, funcPtr> map;

I fill it like this, it seems to work:

map.insert(std::make_pair(std::make_pair(val11, val21), &MyClass::init));

But I can't access to the function like this:

map[std::make_pair<val11, val21>]();

What am I doing wrong?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Elfayer
  • 4,411
  • 9
  • 46
  • 76

1 Answers1

2

You are using the wrong parentheses with make_pair and need to call the member function on some instance of MyClass (using the .* or ->* operators):

MyClass obj;
(obj.*map[std::make_pair(val11, val21)])();
Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324