-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_pointer.cpp
More file actions
37 lines (31 loc) · 818 Bytes
/
function_pointer.cpp
File metadata and controls
37 lines (31 loc) · 818 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<iostream>
#include<map>
#include<string>
using namespace std;
int add (int a,int b) {
return a + b;
}
int sub (int a,int b) {
return a - b;
}
int mult (int a,int b) {
return a * b;
}
int div (int a,int b) {
return a / b;
}
int main(){
typedef int (*cmd_handler_t)(int,int);
map<string,cmd_handler_t> function_map;
map<string,int(*)(int,int)> function_map1; //very difficult way to use the function pointer
function_map["do_add"] = &add;
function_map["do_sub"] = ⊂
function_map["do_mult"] = &mult;
function_map["do_div"] = ÷
function_map1["do_add"] = &add;
//use of function pointer
int t = function_map["do_add"](12,23);
cout <<"function pointer map add result = "<< t << endl;
t = function_map1["do_add"](12,34);
cout <<"function pointer map1 add result = "<< t << endl;
}