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