fork(1) download
  1. #include <iostream>
  2. #include <functional>
  3.  
  4. using namespace std;
  5.  
  6. typedef std::function<void()> CallBack;
  7.  
  8. class TestChangeCallback
  9. {
  10. public:
  11. void Do(const int& a)
  12. {
  13. cout << "Before: " << a << endl;
  14. m_Callback = std::bind(&TestChangeCallback::Do, this, 2);
  15. cout << "After: " << a << endl;
  16. }
  17.  
  18. void Invoke()
  19. {
  20. cout << __FUNCTION__ << " start " << bool(m_Callback)<< endl;
  21. if (m_Callback)
  22. {
  23. m_Callback();
  24. }
  25. cout << __FUNCTION__ << " end " << bool(m_Callback)<< endl;
  26. }
  27.  
  28. CallBack m_Callback;
  29. };
  30.  
  31. int main() {
  32. TestChangeCallback test;
  33.  
  34. test.m_Callback = std::bind(&TestChangeCallback::Do, &test, 1);
  35. test.Invoke();
  36. cout << __FUNCTION__ << endl;
  37.  
  38. return 0;
  39. }
  40.  
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
Invoke start 1
Before: 1
After: 1
Invoke end 1
main