-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallback.cpp
47 lines (37 loc) · 853 Bytes
/
callback.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
//http://www.cplusplus.com/forum/beginner/196638/
#include <iostream>
#include <string>
#include <functional>
class A
{
public:
A() = default;
~A() = default;
void BindCallback(std::function<int(void*,int, int)> fn)
{
privateCallback = std::bind(fn, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
}
void MakeCallback(int i, int j)
{
if (privateCallback)
printf("Output from callback: %d\n", privateCallback((void*)m_pCaller, i, j));
}
void SetCaller(void* pCaller)
{
m_pCaller = pCaller;
}
private:
std::function<int(void*,int, int)> privateCallback;
void* m_pCaller;
};
int f(void* p, int a, int b) {
return (a + b);
}
int main()
{
A obj;
obj.BindCallback(f);
obj.MakeCallback(3, 4);
obj.MakeCallback(7, 8);
return 0;
}