Skip to content

Instantly share code, notes, and snippets.

@finlaybob
Created August 5, 2012 11:50
Show Gist options
  • Select an option

  • Save finlaybob/3264139 to your computer and use it in GitHub Desktop.

Select an option

Save finlaybob/3264139 to your computer and use it in GitHub Desktop.
std::function<> and lambdas
#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