C++基础 -2- 命名空间

———————命名空间———————

 

🎄创建命名空间格式(图片+代码段)

 

namespace rlxy
{
    int a = 10;
    char b;
    double c;
}

  

🎄访问命名空间格式

 

rlxy::a 

  

🎄访问不同命名空间的相同变量

int main()
{
    cout << rlxy::a << endl;
    cout << cpdd::a << endl;
    cout << a << endl;
}

  

🎄 空间暴露

 

 

using namespace rlxy;

  

🎄只暴露空间中的某一成员

 

using  rlxy::a;

  

🎄命名空间的嵌套

 

namespace cpdd1
{
    namespace cpdd2
    {
        namespace cpdd3
        {
            int a=99;
        }
    }
}

  

🎄嵌套命名空间成员的访问

 

cout << cpdd1::cpdd2::cpdd3::a << endl;

🎄嵌套命名空间成员整个暴露

 

using namespace cpdd1;
using namespace cpdd2;
using namespace cpdd3;

  

🎄嵌套命名空间成员单个暴露

 

using cpdd1::cpdd2::cpdd3::a;

———————End———————

 

posted @ 2023-12-10 15:24  日落悬崖  阅读(12)  评论(0)    收藏  举报