fork download
  1. #include <iostream>
  2. #include <functional>
  3. using namespace std;
  4.  
  5. struct Callback{
  6. std::function<void(void)> func;
  7. Callback* next = nullptr;
  8.  
  9. Callback* call(){
  10. func();
  11. return next;
  12. }
  13. };
  14.  
  15. void foo(){
  16. cout << "foo\n";
  17. }
  18.  
  19. void bar(){
  20. cout << "bar\n";
  21. }
  22.  
  23. int main() {
  24. Callback fooCaller{foo,nullptr};
  25. Callback barCaller{bar,&fooCaller};
  26. fooCaller.next=&barCaller;
  27.  
  28. Callback* nextCaller = &fooCaller;
  29. for(int i=0; i<10;++i)
  30. nextCaller = nextCaller->call();
  31. // your code goes here
  32. return 0;
  33. }
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
foo
bar
foo
bar
foo
bar
foo
bar
foo
bar