Skip to content

名字控制

创建名字是程序设计过程中一项最基本的活动,当一个项目很大时,它会不可避免地包含大量名字。C++允许我们对名字的产生和名字的可见性进行控制。

我们之前在学习C语言可以通过static关键字来使得名字只得在本编译单元内可见,在C++中我们将通过一种通过命名空间来控制对名字的访问。

命名空间

在C++中,名称(name)可以是符号常量、变量、函数、结构、枚举、类和对象等等。工程越大,名称互相冲突性的可能性越大。另外使用多个厂商的类库时,也可能导致名称冲突。为了避免在大规模程序的设计中以及在程序员使用各种各样的C++库时这些标识符的命名发生冲突,标准C++引入关键字namespace(命名空间/名字空间/名称空间),可以更好地控制标识符的作用域。

  • 命名空间用途:
  • 命名空间下可以存放 : 变量、函数、结构体、类…

命名空间具有以下特性

  • 命名空间必须要声明在全局作用域
  • 命名空间可以嵌套命名空间
  • 命名空间是开放的,可以随时将新成员添加到命名空间下
  • 命名空间可以匿名的
  • 命名空间可以起别名

使用语法

  • 创建一个命名空间

    点击查看代码示例
    cpp
    #include <iostream>
    using namespace std;
    
    namespace A{
      int a = 10;
    }
    
    namespace B{
      int a = 20;
    }
    
    int main(int argc, char const *argv[])
    {
      cout << "A::a = " << A::a << endl;
      cout << "B::a = " << B::a << endl;
    
      return 0;
    }

    程序输出:

    shell
    A::a = 10
    B::a = 20
  • 点击查看示例
    c
    void test() {
        //在函数体内部不允许创建命名空间
      namespace A {
        int a = 10;
      }
        //IDE直接对命名空间A、B报错,程序无法进行编译
      namespace B {
        int a = 20;
      }
        
      cout << "A::a : " << A::a << endl;
      cout << "B::a : " << B::a << endl;
    }

  • 命名空间可嵌套命名空间

    点击查看代码示例
    cpp
    #include <iostream>
    using namespace std;
    
    namespace A{
      int a = 10;
    
      // 命名空间可以嵌套
      namespace B{
      int a = 20;
      }
    }
    
    
    int main(int argc, char const *argv[])
    {
      cout << "A::a = " << A::a << endl;
      cout << "A::B::a = " << A::B::a << endl;
    
      return 0;
    }

    程序输出:

    shell
    A::a = 10
    A::B::a = 20
  • 命名空间是开放的,即可以随时把新的成员加入已有的命名空间中

    点击查看代码示例
    cpp
    #include <iostream>
    using namespace std;
    
    namespace A{
      int a = 10;
    }
    
    /* 当命名空间A没有被定义的情况下,下面的语句会创建一个name为A的命名空间;当命名空间A已经被创建了,下面的语句会往命名空间A中追加内容 */
    //然后尝试往A中添加内容
    namespace A {
      void func() {
        cout << "hello namespace!" << endl;
      }
    }
    
    
    int main(int argc, char const *argv[])
    {
      cout << "A::a : " << A::a << endl;
      A::func();
    
      return 0;
    }

    程序输出:

    shell
    A::a : 10
    hello namespace!
  • 声明和实现可分离

    点击查看代码示例
    cpp
    #pragma once
    namespace MySpace {
      void func1();
      void func2(int param);
    }
    cpp
    #include <iostream>
    #include "head.h"
    using namespace std;
    
    void MySpace::func1() {
      cout << "MySpace::func1" << endl;
    }
    
    void MySpace::func2(int param) {
      cout << "MySpace::func2 : " << param << endl;
    }
    
    int main(int argc, char *argv[]) {
      MySpace::func1();
      MySpace::func2(10);
    
      return 0;
    }
    shell
    MySpace::func1
    MySpace::func2 : 10
  • 无名命名空间,意味着命名空间中的标识符只能在本文件内访问,相当于给这个标识符加上了static关键字,使得其可以作为内部连接。

    点击查看代码示例
    cpp
    #include <iostream>
    using namespace std;
    
    namespace {
      int a = 10;
      void func() { cout << "hello namespace" << endl; }
    }
    
    int main(int argc, char *argv[]) {
      cout << "a : " << a << endl;
      func();
    
      return 0;
    }

    程序输出:

    shell
    MySpace::func1
    MySpace::func2 : 10
  • 命名空间别名:命名空间的别名并不是通过typedef来定义的,直接使用namespace进行定义别名。

    点击查看代码示例
    cpp
    #include <iostream>
    using namespace std;
    
    namespace veryLongName {
      int a = 10;
      void func() { cout << "hello namespace" << endl; }
    }
    
    int main(int argc, char *argv[]) {
      namespace shortName = veryLongName;
      cout << "veryLongName::a : " << shortName::a << endl;
      veryLongName::func();
      shortName::func();
    
      return 0;
    }

    程序输出:

    shell
    veryLongName::a : 10
    hello namespace
    hello namespace

using声明

using声明可使得指定的标识符可用。语法:using xxx::xxx;

点击查看代码示例
cpp
#include <iostream>
using namespace std;

namespace A {
  int paramA = 20;
  int paramB = 30;
  void funcA() { cout << "hello funcA" << endl; }
  void funcB() { cout << "hello funcA" << endl; }
}

int main(int argc, char *argv[]) {
  //1. 通过命名空间域运算符
  cout << A::paramA << endl;
  A::funcA();

  //2. using声明
  using A::paramA;
  using A::funcA;
  cout << paramA << endl;
  //cout << paramB << endl; //不可直接访问
  funcA();

  //3. 同名冲突
  //int paramA = 20; //相同作用域注意同名冲突
  
  return 0;
}

程序输出:

shell
20
hello funcA
20
hello funcA
,例如:
点击查看代码示例
cpp
#include <iostream>
using namespace std;

namespace A{
    int a = 10;
}

int main(int argc, char *argv[]){
    //使用using声明访问A里面的a
    using A::a;
    cout << a << endl;
    
    //如果现在在main函数作用域内在定义一个同名的a
    //此时出现就近原则,按照以往想法打印的是main里面定义的a
    //但事实是两个a都不可以访问,编译失败
    int a = 20;
    //cout << a << endl;
    
    //即使通过命名空间域运算符来访问,仍然是失败的
    //编程时需要特别注意
    //cout << A::a << endl;
    
    return 0;
}

using声明碰到函数重载 如果命名空间包含一组用相同名字重载的函数,using声明就声明了这个重载函数的所有集合。

点击查看代码示例
cpp
#include <iostream>
using namespace std;

namespace A {
  void func() { 
    cout << "func()" << endl; 
  }

  void func(int x) { 
    cout << "func(x),x=" << x << endl; 
  }

  int  func(int x, int y) {
    cout << "func(x,y),x+y=" << x + y << endl;
    return x + y; 
  }
}

int main(int argc, char *argv[]) {
  using A::func;
  func();
  func(10);
  func(10, 20);
  
  return 0;
}

程序输出:

cpp
func()
func(x),x=10
func(x,y),x+y=30

using编译指令

using编译指令使整个命名空间标识符可用。语法:using namespace xxx;

点击查看代码示例
cpp
#include <iostream>
using namespace std;

namespace A {
  int paramA = 20;
  int paramB = 30;
  void funcA() { cout << "hello funcA" << endl; }
  void funcB() { cout << "hello funcB" << endl; }
}

void test01() {
  using namespace A;
  cout << paramA << endl;
  cout << paramB << endl;
  funcA();
  funcB();
  //不会产生二义性
  int paramA = 30;
  cout << paramA << endl;
}

namespace B {
  int paramA = 20;
  int paramB = 30;
  void funcA() { cout << "hello funcA" << endl; }
  void funcB() { cout << "hello funcB" << endl; }
}

void test02() {
  using namespace A;
  using namespace B;
  //二义性产生,不知道调用A还是B的paramA
  //cout << paramA << endl;
    //如果此时要访问,需要使用命名空间作用域运算符区分
}

int main(int argc, char *argv[]) {
  test01();
  test02();

  
  return 0;
}

注意


点击查看代码示例
cpp
#include <iostream>
using namespace std;

namespace A{
    int a = 10;
}

int main(int argc, char *argv[]){
    int a = 20;
    
    //使用using编译指令
    using namespace A;
    cout << a << endl;
    
    
    return 0;
}

程序输出:

shell
20

总结

我们刚讲的一些东西一开始会觉得难一些,这些东西以后还是挺常用,只要理解了它们的工作机理,使用它们非常简单。

需要记住的关键问题是当引入一个全局的using编译指令时,就为该文件打开了该命名空间,它不会影响任何其他的文件,所以可以在每一个实现文件中调整对命名空间的控制。比如,如果发现某一个实现文件中有太多的using指令而产生的命名冲突,就要对该文件做个简单的改变,通过明确的限定或者using声明来消除名字冲突,这样不需要修改其他的实现文件。