fork download
  1. #include <iostream>
  2. #include <functional>
  3.  
  4. using CallbackType = std::function<void()>;
  5.  
  6. class Car {
  7. public:
  8. void car_run(void);
  9. void car_stop(void);
  10.  
  11. CallbackType f1;
  12.  
  13. private:
  14. CallbackType callbackHandler;
  15. };
  16.  
  17. void Car::car_run(void)
  18. {
  19. std::cout<<"Car running"<<std::endl;
  20. }
  21.  
  22. void Car::car_stop(void)
  23. {
  24. std::cout<<"Car stopping"<<std::endl;
  25. }
  26.  
  27. class Boat {
  28. public:
  29. Boat(Car& car_itf);
  30. static void boat_run(void);
  31. static void boat_stop(void);
  32. private:
  33. Car& car_itf_;
  34. };
  35.  
  36. Boat::Boat(Car& car_itf):car_itf_{car_itf}{
  37. car_itf_.f1 = std::bind(&Boat::boat_run);
  38. }
  39.  
  40. void Boat::boat_run(void)
  41. {
  42. std::cout<<"Boat running"<<std::endl;
  43. }
  44.  
  45. void Boat::boat_stop(void)
  46. {
  47. std::cout<<"Boat running"<<std::endl;
  48. }
  49.  
  50. int main()
  51. {
  52. Car bmw;
  53. Boat ferry(bmw);
  54. bmw.car_run();
  55. bmw.f1();
  56.  
  57. return 0;
  58. }
Success #stdin #stdout 0.01s 5512KB
stdin
Standard input is empty
stdout
Car running
Boat running