-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpointer.cpp
More file actions
57 lines (42 loc) · 703 Bytes
/
pointer.cpp
File metadata and controls
57 lines (42 loc) · 703 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* pointer.cpp
* 2022-06-01 K.OHWADA
*/
// function pointer
// https://qiita.com/kurun_pan/items/f2edd3b834dcaeaa8b2f
#include <iostream>
#include <memory>
/**
* class Calc
*/
class Calc {
public:
Calc() = default;
~Calc() = default;
void setMethod(int (*method)(int a, int b)) {
method_ = method;
}
int invokeMethod(int a, int b) {
if (method_)
return method_(a, b);
return -1;
}
private:
int (*method_)(int a, int b);
};
/**
* add
*/
int add(int a, int b) {
return a + b;
}
/**
* main
*/
int main()
{
auto calc = std::make_unique<Calc>();
calc->setMethod(add);
std::cout << calc->invokeMethod(1, 2) << std::endl;
return 0;
}