example4_nested_context.cpp

 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
#include <iostream>

int globalVar = 100;

void outerFunction() {
    int outerVar = 10;
    
    auto innerFunction = [&]() {
        int innerVar = 1;
		globalVar = 200;
        std::cout << "In innerFunction: "
                  << "globalVar = " << globalVar << ", "
                  << "outerVar = " << outerVar << ", "
                  << "innerVar = " << innerVar <<  ", "
                  << "globalVar = " << globalVar << std::endl;
    };
    
    innerFunction();
    std::cout << "In outerFunction: "
              << "globalVar = " << globalVar << ", "
              << "outerVar = " << outerVar << std::endl;
}

int main() {
    outerFunction();
    std::cout << "In main: globalVar = " << globalVar << std::endl;
    return 0;
}
Back to context