Skip to content

Instantly share code, notes, and snippets.

@vdudouyt
Created November 18, 2014 06:45
Show Gist options
  • Save vdudouyt/9ac7595a2b61738e176e to your computer and use it in GitHub Desktop.
Save vdudouyt/9ac7595a2b61738e176e to your computer and use it in GitHub Desktop.
#include <functional>
using namespace std;
class Test
{
public:
Test();
void say_hello(string way);
private:
typedef void (Test::*hellomethod_t) (string name);
map <string, hellomethod_t> methods;
void hello1(string name) { cout << "hello1, " << name << endl; }
void hello2(string name) { cout << "hello2, " << name << endl; }
};
Test::Test()
{
methods["way1"] = &Test::hello1;
}
void Test::say_hello(string way)
{
hellomethod_t fn = methods[way];
(this->*fn)(string("Bob"));
}
int main()
{
Test t;
t.say_hello("way1");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment