Created
November 18, 2014 06:45
-
-
Save vdudouyt/9ac7595a2b61738e176e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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