Created
August 5, 2012 11:50
-
-
Save finlaybob/3264139 to your computer and use it in GitHub Desktop.
std::function<> and lambdas
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 <map> | |
| #include <functional> | |
| #include <string> | |
| #include <iostream> | |
| using namespace std; | |
| function<void(string,string)> func; | |
| void actualABFunc(string a, string b){ | |
| string ab = a;ab+=b; | |
| cout << ab << endl; | |
| } | |
| void actualBAFunc(string a, string b){ | |
| string ba = b;ba+=a; | |
| cout << ba << endl; | |
| } | |
| class Game | |
| { | |
| public: | |
| Game(){ | |
| } | |
| void requiredMethod(string a, string b){ | |
| string s; | |
| for (int i=0;i<5;i++) | |
| s+=a; | |
| for (int i=0;i<5;i++) | |
| s+=b; | |
| cout << s << endl; | |
| } | |
| }; | |
| void main(int _Argc, char** _Argv){ | |
| func = actualABFunc; | |
| func("a","b"); | |
| func = actualBAFunc; | |
| func("a","b"); | |
| Game* g = new Game(); | |
| Game game; | |
| func = [&](string a,string b) { g->requiredMethod(a,b); } ; | |
| func("a","b"); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment