https://rentry.org/PPP2_p270 ⎗ ✓ 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// Code derived from Stroustrup's PPP2 book // § 8.4 Scope // -and beginning on p 270 int g(int x) // g is global; x is local to g { int f = x + 2; // f is local return 2 * f; } class C { public: struct M { // ... }; // ... }; void f() { class L { // ... }; // ... } void f1() { void g() // illegal { // ... }... } void f2(int x, int y) { if (x > y) { // ... } else { // ... { // ... } // ... } } //------------------------------------------------------------------------------ // clang-format off // dangerously ugly code: struct X { void f(int x) { struct Y { int f() { return 1; } int m; }; int m; m=x; Y m2; return f(m2.f()); } int m; void g(int m) { if (m) f(m+2); else { g(m+2); }} X() { } void m3() { } void main() { X a; a.f(2);} }; // clang-format on //------------------------------------------------------------------------------ int main() { g(0); f(); f2(0, 1); X x; x.main(); x.m3(); } build & run: g++ -std=c++20 -O2 -Wall -pedantic ./ch_08/main_p270.cpp && ./a.out sauce: Bjarne Stroustrup's PPP2 textbook /robowaifu/'s official C++ learning textbook thread Prev • Up • Next