fork(31) download
  1. #include <functional>
  2. #include <iostream>
  3.  
  4.  
  5. class Test
  6. {
  7. public:
  8. void blah() { std::cout << "BLAH!" << std::endl; }
  9. };
  10.  
  11. class Bim
  12. {
  13. public:
  14. void operator()(){ std::cout << "BIM!" << std::endl; }
  15. };
  16.  
  17. void boum() { std::cout << "BOUM!" << std::endl; }
  18.  
  19.  
  20. int main()
  21. {
  22. // store the member function of an object:
  23. Test test;
  24. std::function< void() > callback = std::bind( &Test::blah, test );
  25. callback();
  26.  
  27. // store a callable object (by copy)
  28. callback = Bim{};
  29. callback();
  30.  
  31. // store the address of a static funtion
  32. callback = &boum;
  33. callback();
  34.  
  35. // store a copy of a lambda (that is a callable object)
  36. callback = [&]{ test.blah(); }; // might be clearer than calling std::bind()
  37. callback();
  38. }
Success #stdin #stdout 0s 3020KB
stdin
Standard input is empty
stdout
BLAH!
BIM!
BOUM!
BLAH!