This question answers how to do it for a regular unordered_map
, but what about the member one? The IDE reports the error in the comment. opcodes
maps characters to pointers to functions, which should be called on behalf of concrete instances. Also I wonder if it's possible to make it constexpr.
// Foo.cpp
class Foo {
public:
void execute(char c) { (*opcodes[c])(); }
void operation1()
{
// Do something with value_
}
void operation2();
private:
typedef void (*operation)();
static const std::unordered_map<char, operation> opcodes{
{'1', &Foo::operation1},
{'2', &Foo::operation2}
}; // No matching constructor for initialization of 'const std::unordered_map<char, operation>' (aka 'const unordered_map<char, void (*)()>')
int value_;
}
// main.cpp
int main()
{
Foo foo;
foo.execute('1');
return 0;
}