pastebin

Paste Search Dynamic
Recent pastes
Widget
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. namespace Foo {
  5.         int x = 1; // A
  6.         int y = 1; // A
  7. }
  8.  
  9. int x = 2; // B
  10. int y = 2; // B
  11. using namespace Foo;
  12.  
  13. //x = 3; // Error: ambiguous
  14.  
  15. struct Widget {
  16.         void bar(int x) {
  17.                 x = 42;    // OK, it's a different `x` anyway, not even this->x.
  18.                 cout << "Widget::x == " << this->x << endl;
  19.                 ::x = 364; // not ambiguous [always refers to instance B]
  20.                            // but irrelevant to the problem being discussed.
  21.                 y = 223322; // Also not ambiguous, refers to C aka this->y
  22.         }
  23.        
  24.         int x{123}; // C
  25.         int y{456}; // C
  26. };
  27.  
  28. int main() {
  29.         Widget{}.bar(100);
  30.         cout << "Foo::x == " << Foo::x << endl;
  31.         cout << "Global x == " << ::x << endl;
  32.  
  33.         cout << "Foo::y == " << Foo::y << endl;
  34.         cout << "Global y == " << ::y << endl;
  35.         return 0;
  36. }
Parsed in 0.007 seconds