callback-test.cpp 998 Bytes
Newer Older
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
58
59
60
61
62
63
64
65
/*
 * callback-test.cpp
 *
 *  Created on: 21.09.2017
 *      Author: geith
 */

#include <iostream>
#include <functional>

using namespace std;

typedef int (callback_t) (int, int);

void
test_function(callback_t cb, int arg1, int arg2){
	cout << "calling callback function with arguments "
			<< arg1 << " and " << arg2 << endl;

	auto rv = cb(arg1, arg2);

	cout << "got result " << rv << endl;
}

class Test{
public:

	int test_cb(int arg1, int arg2){
		return arg1 + arg2;
	}

	void doCallbackTest(){
		int a1 = 5;
		int a2 = 7;

#if 0
		std::function<int(int, int)> func([this](int arg1, int arg2) -> int{
			return this->test_cb(arg1, arg1);
		});
		test_function(func, a1, a2);
#else
		/*
		test_function([this](int arg1, int arg2) -> int{
			return this->test_cb(arg1, arg1);
		}, a1, a2);
		*/
#endif
	}

protected:

};


int main(int argc, char** argv){
	cout << "starting callback test" << endl;

	Test t;

	t.doCallbackTest();


	cout << "finished callback test" << endl;
	return 0;
}