-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathLambda03.cpp
More file actions
62 lines (46 loc) · 1.53 KB
/
Lambda03.cpp
File metadata and controls
62 lines (46 loc) · 1.53 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
// =====================================================================================
// Lambda03.cpp // Lambda and 'this' Closure
// =====================================================================================
module modern_cpp:lambda;
namespace LambdaAndThisClosure {
class Class {
friend std::ostream& operator << (std::ostream&, const Class&);
private:
int m_value;
public:
Class() : m_value{ 1 } {}
// just for demonstration purposes / observe, when copy c'tor is called !!!
Class(const Class& obj) {
std::cout << "copy c'tor called ..." << std::endl;
m_value = obj.m_value;
}
void incValue() {
++m_value;
}
void doSomething() {
auto lambda = [this]() mutable { // use [*this] to work on a copy
incValue();
return m_value;
};
lambda();
}
};
std::ostream& operator << (std::ostream& os, const Class& obj) {
os << "m_value = " << obj.m_value;
return os;
}
static void test_01() {
Class object;
std::cout << object << std::endl;
object.doSomething();
std::cout << object << std::endl;
}
}
void main_lambdas_this_closure()
{
using namespace LambdaAndThisClosure;
test_01();
}
// =====================================================================================
// End-of-File
// =====================================================================================