Last active
October 8, 2015 01:48
-
-
Save finlaybob/3258819 to your computer and use it in GitHub Desktop.
My code from learning about function pointers.
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 <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