有名字的命名空间

命名空间的分类

  • 有名字的命名空间
  • 匿名命名空间

有名字的命名空间的定义:

namespace 命名空间名    
{
    int a;
    double b;
    ...
}

使用有名字的命名空间中的变量:

使用::引用命名空间的变量等。

例如:

ns::a =100;
ns::b =3.14;

example:

#include <iostream>
//定义一个有名称的命名空间,名字为ns
namespace ns {
   //定义变量
    int a=100;
    double b=3.14;
}
int main(int argc, const char * argv[]) {
    //使用命名空间ns中的变量
    std::cout<<ns::a<<std::endl;
    std::cout<<ns::b<<std::endl;
    return 0;
}
100
3.14

results matching ""

    No results matching ""