fork(1) download
  1. #include <functional>
  2. #include <iostream>
  3.  
  4. class Tracker
  5. {
  6. public:
  7. Tracker() : _methodCalled(false) {}
  8.  
  9. void MethodToCall()
  10. {
  11. std::cout << "Method called\n";
  12. _methodCalled = true;
  13. }
  14.  
  15. bool GetMethodCalled() const
  16. {
  17. return _methodCalled;
  18. }
  19.  
  20. private:
  21. bool _methodCalled;
  22. };
  23.  
  24. int main()
  25. {
  26. Tracker trackerToCopy;
  27. auto boundToCopy = std::bind(&Tracker::MethodToCall, trackerToCopy);
  28.  
  29. std::cout << "Bound to copy of tracker\n\t";
  30. boundToCopy();
  31. std::cout << "\t_methodCalled value: " << (trackerToCopy.GetMethodCalled() ? "true" : "false") << std::endl;
  32.  
  33. Tracker trackerLocalInstance;
  34. auto boundToLocalInstance = std::bind(&Tracker::MethodToCall, &trackerLocalInstance);
  35.  
  36. std::cout << "Bound to local instance of tracker\n\t";
  37. boundToLocalInstance();
  38. std::cout << "\t_methodCalled value: " << (trackerLocalInstance.GetMethodCalled() ? "true" : "false") << std::endl;
  39.  
  40. Tracker trackerReference;
  41. auto boundToReference = std::bind(&Tracker::MethodToCall, std::ref(trackerReference));
  42.  
  43. std::cout << "Bound to refrence to tracker\n\t";
  44. boundToReference();
  45. std::cout << "\t_methodCalled value: " << (trackerReference.GetMethodCalled() ? "true" : "false") << std::endl;
  46.  
  47. return 0;
  48. }
Success #stdin #stdout 0s 2896KB
stdin
Standard input is empty
stdout
Bound to copy of tracker
	Method called
	_methodCalled value: false
Bound to local instance of tracker
	Method called
	_methodCalled value: true
Bound to refrence to tracker
	Method called
	_methodCalled value: true