-
Notifications
You must be signed in to change notification settings - Fork 33
/
FunctionPointer.cpp
57 lines (45 loc) · 955 Bytes
/
FunctionPointer.cpp
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
class Example {
public:
Example() {}
~Example() {}
int add(int a, int b) {
return a + b;
}
int mul(int a, int b) {
return a * b;
}
};
typedef int (Example::*Fxn)(int, int);
int main() {
Example* ptr(NULL);
ptr = new Example();
if (ptr == NULL) {
return -1;
}
Fxn fxn(NULL);
unsigned int selection(0);
std::cout << "Make selection (0) add, (1) mul: \n";
std::cin >> selection;
switch (selection) {
case 0:
fxn = &Example::add;
break;
case 1:
fxn = &Example::mul;
break;
}
if (fxn != NULL) {
int a(0), b(0);
std::cout << "Enter A: ";
std::cin >> a;
std::cout << "Enter B: ";
std::cin >> b;
std::printf("%d\n", (ptr->*fxn)(a, b));
}
if (ptr != NULL) {
delete ptr;
ptr = NULL;
}
return 0;
}