fork(1) download
  1. #include <iostream>
  2. #include <functional> // C++11
  3. //#include <boost/function.hpp>
  4. //#include <boost/bind.hpp>
  5.  
  6. struct A{
  7. std::function<void(int)> callback;
  8. void invoke(){ callback(42); }
  9. };
  10.  
  11. struct B{
  12. void foo(int data){ std::cout << data * 2 << "\n"; }
  13. };
  14.  
  15. struct C{
  16. void bar(int data){ std::cout << data / 2 << "\n"; }
  17. };
  18.  
  19. int main(){
  20. using namespace std::placeholders; // namespace for argument placeholders for std::bind
  21. // not needed for Boost.Bind
  22. A a;
  23. B b;
  24. a.callback = std::bind(&B::foo, &b, _1);
  25. a.invoke();
  26. C c;
  27. a.callback = std::bind(&C::bar, &c, _1);
  28. a.invoke();
  29. };
Success #stdin #stdout 0s 3020KB
stdin
Standard input is empty
stdout
84
21