fork download
  1. #include <iostream>
  2. #include <functional>
  3. int add(int a, int b){return a+b;}
  4. int sub(int a, int b){return a-b;}
  5. int mul(int a, int b){return a*b;}
  6.  
  7. int main()
  8. {
  9. // array of pointers to functions
  10. int (*arr1[])(int, int) = {add, sub, mul};
  11. // array of std::reference_wrappers
  12. std::reference_wrapper<int(int, int)> arr2[] = {std::ref(add), std::ref(sub), std::ref(mul)};
  13. // array of std::functions
  14. std::function<int(int,int)> arr3[] = {add, sub, mul};
  15.  
  16. // let's use them
  17. std::cout << arr1[0](1, 2) << ' ' << arr1[1](1,2) << ' ' << arr1[2](1,2) << '\n'
  18. << arr2[0](1, 2) << ' ' << arr2[1](1,2) << ' ' << arr2[2](1,2) << '\n'
  19. << arr3[0](1, 2) << ' ' << arr3[1](1,2) << ' ' << arr3[2](1,2) << '\n';
  20. }
  21.  
Success #stdin #stdout 0s 2884KB
stdin
Standard input is empty
stdout
3 -1 2
3 -1 2
3 -1 2