C++演示在不同的范围内不冲突使用同名变量的范例的代码

C++演示在不同的范围内不冲突使用同名变量的范例的代码

把做工程过程中经常用的内容段珍藏起来,下边资料是关于C++演示在不同的范围内不冲突使用同名变量的范例的内容。

#include <iostream>

using std::cout;
using std::endl;

{
    public:
};

{
}

{
    return x;
}

int main()
{
    abc a;

    cout << "Scope within class abc:  x = " << a.x
         << "nScope within namespace def:  x = " << def::x
         << "nScope within function getx:  x = " << getx()
         << "nScope within global scope :  x = " << ::x
         << "nScope within local main function:  x = " << x;

         {
             cout << "nScope within local code block:  x = " << x;
         }

    return 0;
}

Scope within class abc:  x = 10
Scope within namespace def:  x = 20
Scope within function getx:  x = 30
Scope within global scope :  x = 40
Scope within local main function:  x = 50
Scope within local code block:  x = 60