1. 程式人生 > >C++演示在不同的範圍內不沖突使用同名變量的範例的代碼

C++演示在不同的範圍內不沖突使用同名變量的範例的代碼

資料 ios space namespace 沖突 ret names func sin

把做工程過程中經常用的內容段珍藏起來,下邊資料是關於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

C++演示在不同的範圍內不沖突使用同名變量的範例的代碼