fork(3) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class A {
  5. typedef void (A::*RunPtr)(int);
  6. RunPtr RunMethod;
  7.  
  8. public:
  9. A()
  10. {
  11. RunMethod = &A::RunOff;
  12. }
  13.  
  14. void SetOn(bool value)
  15. {
  16. if (value)
  17. RunMethod = &A::RunOn;
  18. else
  19. RunMethod = &A::RunOff;
  20. }
  21.  
  22. void RunOn(int param)
  23. {
  24. //RunOn stuff here
  25. cout << "RunOn: " << param << endl;
  26. }
  27.  
  28. void RunOff(int param)
  29. {
  30. //RunOff stuff here
  31. cout << "RunOff: " << param << endl;
  32. }
  33.  
  34. void Run(int param)
  35. {
  36. (this->*RunMethod)(param);
  37. }
  38. };
  39.  
  40. int main() {
  41. A a;
  42. a.SetOn(true);
  43. a.Run(1);
  44. a.SetOn(false);
  45. a.Run(2);
  46. return 0;
  47. }
Success #stdin #stdout 0s 5604KB
stdin
Standard input is empty
stdout
RunOn: 1
RunOff: 2