fork download
  1. #include <functional>
  2. #include <iostream>
  3.  
  4. struct foo
  5. {
  6. int _a;
  7. foo(int a) : _a(a) {}
  8. int operator()(int b) { return _a+b; }
  9. };
  10.  
  11.  
  12. class bar
  13. {
  14. public:
  15. std::function<int (int)> _ftor;
  16.  
  17. bar(std::function<int (int)> my_ftor) : _ftor(my_ftor) {}
  18. ~bar() {}
  19. };
  20.  
  21. void AnyFunction(bar& RefToBar)
  22. {
  23. int y = RefToBar._ftor(25);
  24. std::cout << "Y: " << y << std::endl;
  25. }
  26.  
  27. int AnotherFunction(int b)
  28. {
  29. return b + 11;
  30. }
  31.  
  32. int main(int argc, char const *argv[])
  33. {
  34. foo MyFoo(20);
  35. bar MyBar(MyFoo);
  36. bar MyBar_2(AnotherFunction);
  37. bar MyBar_3([](int b) { return b + 56; });
  38.  
  39.  
  40. AnyFunction(MyBar);
  41. AnyFunction(MyBar_2);
  42. AnyFunction(MyBar_3);
  43. return 0;
  44. }
  45.  
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
Y: 45
Y: 36
Y: 81