fork download
  1. #include <iostream>
  2. #include <functional>
  3.  
  4. class Fred {
  5. public:
  6. void f() { std::cout << "f" << std::endl; }
  7. void g() { std::cout << "g" << std::endl; }
  8. void h() { std::cout << "h" << std::endl; }
  9. void i() { std::cout << "i" << std::endl; }
  10.  
  11. };
  12.  
  13. typedef void (Fred::*FredMemFn)();
  14.  
  15. FredMemFn a[] = { &Fred::f, &Fred::g, &Fred::h, &Fred::i };
  16.  
  17. void userCode(Fred& fred, int memFnNum)
  18. {
  19. std::function<void()> fn = std::bind(a[memFnNum], &fred);
  20.  
  21. fn();
  22.  
  23. }
  24.  
  25. int main() {
  26.  
  27. Fred fred;
  28.  
  29. userCode(fred, 1);
  30. userCode(fred, 3);
  31. userCode(fred, 0);
  32. userCode(fred, 2);
  33.  
  34. return 0;
  35. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
g
i
f
h