Although this is based on Sterling's answers elsewhere on this page, I had a use case that was not completely resolved by them; for a vector of function pointers, do the following:
#include <iostream> #include <vector> #include <stdio.h> #include <stdlib.h> class A{ public: typedef vector<int> (A::*AFunc)(int I1,int I2); vector<AFunc> FuncList; inline int Subtract(int I1,int I2){return I1-I2;}; inline int Add(int I1,int I2){return I1+I2;}; ... void Populate(); void ExecuteAll(); }; void A::Populate(){ FuncList.push_back(&A::Subtract); FuncList.push_back(&A::Add); ... } void A::ExecuteAll(){ int In1=1,In2=2,Out=0; for(size_t FuncId=0;FuncId<FuncList.size();FuncId++){ Out=(this->*FuncList[FuncId])(In1,In2); printf("Function %ld output %d\n",FuncId,Out); } } int main(){ A Demo; Demo.Populate(); Demo.ExecuteAll(); return 0; }
Something like this is useful if you are writing a shell with indexed functions that should be married to parameter syntax and hint tips, etc. Perhaps this is also useful on the menu.
Owl Apr 12 '17 at 22:00 2017-04-12 22:00
source share