-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionPtr.cpp
More file actions
81 lines (67 loc) · 1.58 KB
/
FunctionPtr.cpp
File metadata and controls
81 lines (67 loc) · 1.58 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* \file FunctionPtr.cpp
* \brief pointer to function
*
* \review
*
* https://isocpp.org/wiki/faq/pointers-to-members
*/
#include <StdStream/StdStream.h>
#include <StdTest/StdTest.h>
#include <Stl.h>
//--------------------------------------------------------------------------------------------------
int
func(const std::string &a_value)
{
std::cout << "func: " << a_value << std::endl;
return 0;
}
int (*funcPtr)(const std::string &) = nullptr;
//--------------------------------------------------------------------------------------------------
class A
{
public:
int
method(const std::string &a_value) const
{
std::cout << "method: " << a_value << std::endl;
return 0;
}
};
int (A::*methodPtr)(const std::string &) const = nullptr;
//--------------------------------------------------------------------------------------------------
int main(int, char **)
{
// function - by name
{
(*func)("by name 1");
func("by name 2");
}
// function - by pointer
{
::funcPtr = &func;
(*::funcPtr)("by pointer 1");
::funcPtr("by pointer 2");
}
// method - by name
{
A a;
a.method("by name");
}
// method - by pointer
{
::methodPtr = &A::method;
A a;
(a.*::methodPtr)("by pointer");
}
return EXIT_SUCCESS;
}
//--------------------------------------------------------------------------------------------------
#if OUTPUT
func: by name 1
func: by name 2
func: by pointer 1
func: by pointer 2
method: by name
method: by pointer
#endif