0

Can we pass functions in array?

12th Oct 2016, 2:35 PM
ZUNAIRA
ZUNAIRA - avatar
1 ответ
+ 1
The answer is yes. Here is an example: http://www.cplusplus.com/forum/beginner/4639/ #include <iostream> using namespace std; typedef int (*IntFunctionWithOneParameter) (int a); int function(int a){ return a; } int functionTimesTwo(int a){ return a*2; } int functionDivideByTwo(int a){ return a/2; } int main() { IntFunctionWithOneParameter functions[] = { function, functionTimesTwo, functionDivideByTwo }; for(int i = 0; i < 3; ++i) { cout << functions[i](8) << endl; } return 0; }
12th Oct 2016, 2:50 PM
Zen
Zen - avatar