Skip to content

Commit df2c36b

Browse files
committed
test10.cpp
1 parent c5620e2 commit df2c36b

1 file changed

Lines changed: 22 additions & 4 deletions

File tree

section2/test10.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// g++ test10.cpp -std=c++14 -I../common -o a.out;./a.out
55

66
#include <iostream>
7+
#include <string>
78
#include <vector>
89
#include <algorithm>
910
#include <functional>
@@ -108,23 +109,39 @@ class DemoLambda final
108109
private:
109110
int x = 0;
110111
public:
111-
void print()
112+
auto print()
112113
{
113114
//auto f = [=]()
114-
auto f = [this]()
115+
116+
return [this]()
115117
{
116118
cout << "member = " << x << endl;
117119
};
118120

119-
f();
120121
}
121122
};
122123

123124
void case5()
124125
{
125126
DemoLambda obj;
126127

127-
obj.print();
128+
auto f = obj.print();
129+
130+
f();
131+
}
132+
133+
void case6()
134+
{
135+
auto f = [](const auto& x)
136+
{
137+
return x + x;
138+
};
139+
140+
cout << f(3) << endl;
141+
cout << f(0.618) << endl;
142+
143+
string str = "matrix";
144+
cout << f(str) << endl;
128145
}
129146

130147
int main()
@@ -134,6 +151,7 @@ int main()
134151
case3();
135152
case4();
136153
case5();
154+
case6();
137155

138156
cout << "lambda demo" << endl;
139157
}

0 commit comments

Comments
 (0)