Created
September 12, 2012 13:52
-
-
Save tomthorogood/3706745 to your computer and use it in GitHub Desktop.
Dynamic function members in C++
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 <string> | |
| #include <iostream> | |
| using namespace std; | |
| typedef string (*callback)(string); | |
| string cb1 (string str) | |
| { | |
| return str + "...in bed!"; | |
| } | |
| string cb2 (string str) | |
| { | |
| return str + " ...that's what she said!"; | |
| } | |
| class Tester | |
| { | |
| string (*exec)(string); | |
| public: | |
| Tester() | |
| { | |
| } | |
| void setExec(callback cb) | |
| { | |
| exec = cb; | |
| } | |
| string run(string str) | |
| { | |
| return exec(str); | |
| } | |
| }; | |
| int main() | |
| { | |
| string foo = "Last night I ate brains."; | |
| Tester tester; | |
| tester.setExec(&cb1); | |
| cout << tester.run(foo) << endl; | |
| tester.setExec(&cb2); | |
| cout << tester.run(foo) << endl; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment