Skip to content

Instantly share code, notes, and snippets.

@finlaybob
Last active October 8, 2015 01:48
Show Gist options
  • Select an option

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

Select an option

Save finlaybob/3258819 to your computer and use it in GitHub Desktop.
My code from learning about function pointers.
#include <map>
#include <iostream>
#include <string>
using namespace std;
typedef void (*FuncPtr)(string,string);
typedef std::map<std::string, FuncPtr> FuncPtrMap;
void abFunc(string a,string b){
string ab = a;
ab += b;
cout << ab << endl;
}
class FPtestClass
{
public:
FuncPtrMap fpm;
FPtestClass(string str)
{
fpm.insert(make_pair(str,&FPtestClass::classABFunc));
}
FuncPtr getFP(string scriptFunc)
{
FuncPtr f;
auto itr = fpm.find(scriptFunc);
if (itr!=fpm.end())
{
f = itr->second;
}else{
f = nullptr;
}
return f;
}
static void classABFunc(string a, string b)
{
string c = a; c+= b;
cout << c << endl;
}
};
int main(int argC,char** argv){
FuncPtrMap m;
FuncPtr testone,testtwo;
m.insert(make_pair("command",&abFunc));
auto itr = m.find("command");
if (itr!=m.end())
{
testone = itr->second;
}
FPtestClass* test = new FPtestClass("list");
testtwo = test->getFP("list");
testone("a","b");
testtwo("x","z");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment