-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeSnap-FunctionObjects.h.html
More file actions
121 lines (105 loc) · 4.76 KB
/
CodeSnap-FunctionObjects.h.html
File metadata and controls
121 lines (105 loc) · 4.76 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<!----------------------------------------------------------------------------
CodeSnap-FunctionObjects.h.htm
Published 19 Mar 2017
Jim Fawcett, CSE687 : Object Oriented Design, Summer 2017
Note:
- Markup characters in the text part, enclosed in <pre>...</pre> have to be
replaced with escape sequences, e.g., < becomes < and > becomes >
- Be careful that you don't replace genuine markup characters with escape
sequences, e.g., everything outside of the <pre>...</pre> section.
----------------------------------------------------------------------------->
<html>
<head>
<script src="js/ScriptsUtilities.js"></script>
<script src="js/ScriptsTemplate.js"></script>
<script src="js/ScriptsKeyboard.js"></script>
<script src="js/ScriptsMenuCpp.js"></script>
<link rel="stylesheet" href="css/StylesTemplate.css" />
<link rel="stylesheet" href="css/StylesMenu.css" />
<style>
h3 {
font-weight: normal;
}
</style>
</head>
<body id="github" onload="initializeMenu()">
<nav>
<div id="navbar"></div>
</nav>
<a id="Next" href="CodeSnap-FunctionObjects.cpp.html">N</a>
<a id="Prev" href="CodeSnap-FunctionObjects.txt.html">P</a>
<navKeys-Container>
<nav-Key id="sKey" onclick="toggleSwipeEvents()">S</nav-Key>
<nav-Key id="rKey" onclick="location.reload()">R</nav-Key>
<nav-Key id="tKey" onclick="scrollPageTop()">T</nav-Key>
<nav-Key id="bKey" onclick="scrollPageBottom()">B</nav-Key>
<nav-Key id="hKey" onclick="helpWin()">H</nav-Key>
<nav-Key id="pKey" onclick="loadPrev()">P</nav-Key>
<nav-Key id="nKey" onclick="loadNext()">N</nav-Key>
</navKeys-Container>
<h3>
<a href="CodeSnap-FunctionObjects.h.html">FunctionObjects.h</a>,
<a href="CodeSnap-FunctionObjects.cpp.html">FunctionObjects.cpp</a>,
<a href="CodeSnap-FunctionObjects.txt.html">FunctionObjects.txt</a>,
<a class="disable" href="*">Code folder</a>
</h3>
<div class="indent">
Function objects are objects that can be invoked.<br />
This demo illustrates the use of function pointers, functors, and lambdas.
</div>
<hr />
<pre class="codeSnap">
#pragma once
/////////////////////////////////////////////////////////////////////////
// FunctionObjects.h - demonstrate function object declar and invoc. //
// //
// Jim Fawcett, CSE687 - Object Oriented Design, Summer 2017 //
/////////////////////////////////////////////////////////////////////////
/*
* Function objects are functions, function pointers, functors, and
* lambdas. They are widely used in C++ code to:
* - quickly define a locally useful function.
* - start threads
* - define callbacks
* - define arguments for template functions, like STL algorithms
*/
#include <string>
#include <functional>
/*---------------------------------------------------------------------*/
/* Function declaration */
void testFunction(size_t lineNo, const std::string& s);
/*---------------------------------------------------------------------*/
/* Declaration of function pointer: */
/* fPtr can point to any function with the appropriate signature */
void(*fPtr)(size_t lineNo, const std::string& s) = nullptr;
/*---------------------------------------------------------------------*/
/* Declaration of a function pointer type */
typedef void(*FPtr)(size_t lineNo, const std::string& s);
/*---------------------------------------------------------------------*/
/* Declaration of an alias for an anonymous function pointer type */
using AFPtr = void(*)(size_t lineNo, const std::string& s);
/*---------------------------------------------------------------------*/
/* Declaration of a functor type */
/* The advantage of functors is that they can store data as */
/* instance members, used to pass to its operator() as arguments. */
class FunctorExample
{
public:
FunctorExample(size_t lineNo) : lineNo_(lineNo) {}
void operator()(const std::string& arg);
private:
size_t lineNo_;
};
/*---------------------------------------------------------------------*/
/* Declares a standard function */
/* Standard functions can bind to any function object. */
/* We will bind to a lambda. They are really just a shortcut */
/* for defining functors, as we show in main. */
std::function<void(const std::string& s)> stdFunc;
</pre>
<!--<div class="photo">
<img src="pictures/CSTstrip.jpg" width="100%" />
</div>-->
<info-bar></info-bar>
</body>
</html>