I need to implement an std::map with pairs. The function pointers are pointers to methods of the same class that owns t
std::map
This is about the simplest I can come up with. Note no error checking, and the map could probably usefully be made static.
#include #include #include using namespace std; struct A { typedef int (A::*MFP)(int); std::map fmap; int f( int x ) { return x + 1; } int g( int x ) { return x + 2; } A() { fmap.insert( std::make_pair( "f", &A::f )); fmap.insert( std::make_pair( "g", &A::g )); } int Call( const string & s, int x ) { MFP fp = fmap[s]; return (this->*fp)(x); } }; int main() { A a; cout << a.Call( "f", 0 ) << endl; cout << a.Call( "g", 0 ) << endl; }