forked from grantrostig/cpp_by_example
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.cpp
More file actions
53 lines (41 loc) · 1.93 KB
/
main.cpp
File metadata and controls
53 lines (41 loc) · 1.93 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
#include <iostream>
using namespace std;
int main() {
//var createGreeting = function(greeting) { // JavaScript version https://aspiringcraftsman.com/2012/02/17/javascript-closures-explained/
//return function(name) {
//document.write(greeting + ', ' + name + '.');
//};
//};
//helloGreeting = createGreeting("Hello");
//howdyGreeting = createGreeting("Howdy");
//helloGreeting("John"); // Hello, John.{
//howdyGreeting("Jane"); // Hello, John.{
//auto greeting_lambda = [] (std::string greeting) {
//auto greeting_lambda = [&] (std::string greeting) { // makes no difference
//auto greeting_lambda = [=] (std::string greeting) { // makes no difference
auto greeting_lambda = [] (std::string & greeting) { // ?
//auto greeting_lambda = [&] (std::string & greeting) { // ?
//auto greeting_lambda = [=] (std::string & greeting) { // ?
//static string my_greeting {greeting};
//auto name_lambda = [&] (std::string name) { cout << my_greeting << ", " << name << endl; }; // why always prints "hello"?
string my_greeting {greeting};
auto name_lambda = [&] (std::string name) { cout << my_greeting << ", " << name << endl; }; // why crashes?
//string my_greeting {greeting};
//auto name_lambda = [=] (std::string name) { cout << my_greeting << ", " << name << endl; };
//auto name_lambda = [&] (std::string name) { cout << my_greeting << ", " << name << endl; };
return name_lambda;
};
// string yo_string {"Yo"};
static string yo_string {"Yo"};
//auto hello_greeting = greeting_lambda("Hello");
//auto howdy_greeting = greeting_lambda("Howdy");
auto yo_greeting = greeting_lambda(yo_string);
//hello_greeting("John");
//hello_greeting("Sally");
//howdy_greeting("John");
//howdy_greeting("Sally");
yo_greeting( "John");
yo_greeting( "Sally");
cout << "###" << endl;
return 0;
}