fork download
  1. #include <iostream>
  2. #include <map>
  3. using namespace std;
  4.  
  5.  
  6.  
  7. class base {
  8.  
  9. protected:
  10. typedef void (base::*fn)() ;
  11. base(){
  12. fn_arr["foo"]=&base::foo;
  13. }
  14. void foo() {
  15. cout << "i'm foo" << endl;
  16. }
  17.  
  18. public:
  19. map<std::string, fn> fn_arr;
  20. };
  21.  
  22. class derived : public base {
  23.  
  24. protected:
  25. void bar() {
  26. cout <<"i'm bar" << endl;
  27. }
  28.  
  29. public:
  30. derived() {
  31. fn_arr["bar"]=static_cast<fn>(&derived::bar);
  32. }
  33. };
  34.  
  35. int main() {
  36. derived d;
  37. void (derived::*fn)()=d.fn_arr["foo"];
  38. (d.*fn)();
  39. void (derived::*fn_)()=d.fn_arr["bar"];
  40. (d.*fn_)();
  41. return 0;
  42. }
Success #stdin #stdout 0s 4368KB
stdin
Standard input is empty
stdout
i'm foo
i'm bar