亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> C语言

C++ 中的命名空间

考虑一种情况,当我们在同一个班级中有两个同名的人 Zara。每当我们需要明确区分他们时,我们必须使用一些额外的信息以及他们的名字,比如地区,如果他们住在不同的地区,或者他们的母亲或父亲的名字等。

在您的 C++ 应用程序中也可能出现同样的情况。例如,您可能正在编写一些代码,其中包含一个名为 xyz() 的函数,而另一个可用的库也具有相同的函数 xyz()。现在编译器无法知道您在代码中引用的是哪个版本的 xyz() 函数。

命名空间 旨在克服这一困难,并用作附加信息来区分不同库中具有相同名称的类似函数、类、变量等。使用命名空间,您可以定义定义名称的上下文。本质上,命名空间定义了一个范围。

定义命名空间

命名空间定义以关键字 namespace 开头 后跟命名空间名称如下 -

namespace namespace_name {
   // code declarations
}

要调用函数或变量的命名空间启用版本,请在命名空间名称前添加 (::),如下所示 -

name::code;  // code could be variable or function.

让我们看看命名空间如何作用于包括变量和函数在内的实体 -

现场演示
#include <iostream>
using namespace std;

// first name space
namespace first_space {
   void func() {
      cout << "Inside first_space" << endl;
   }
}

// second name space
namespace second_space {
   void func() {
      cout << "Inside second_space" << endl;
   }
}

int main () {
   // Calls function from first name space.
   first_space::func();
   
   // Calls function from second name space.
   second_space::func(); 

   return 0;
}

如果我们编译并运行上面的代码,这将产生以下结果 -

Inside first_space
Inside second_space

using 指令

您还可以避免使用 using namespace 预先添加命名空间 指示。该指令告诉编译器后续代码正在使用指定命名空间中的名称。因此,以下代码隐含了命名空间 -

现场演示
#include <iostream>
using namespace std;

// first name space
namespace first_space {
   void func() {
      cout << "Inside first_space" << endl;
   }
}

// second name space
namespace second_space {
   void func() {
      cout << "Inside second_space" << endl;
   }
}

using namespace first_space;
int main () {
   // This calls function from first name space.
   func();
   
   return 0;
}

如果我们编译并运行上面的代码,这将产生以下结果 -

Inside first_space

‘using’ 指令也可用于引用命名空间中的特定项目。例如,如果您打算使用的 std 命名空间的唯一部分是 cout,您可以按如下方式引用它 -

using std::cout;

后续代码可以引用 cout 而不加命名空间,但 std 中的其他项 命名空间仍然需要明确如下 -

现场演示
#include <iostream>
using std::cout;

int main () {
   cout << "std::endl is used with std!" << std::endl;
   
   return 0;
}

如果我们编译并运行上面的代码,这将产生以下结果 -

std::endl is used with std!

使用中引入的名称 指令遵守正常范围规则。从 using 的角度可以看到名称 指令到指令所在范围的末尾。隐藏在外部作用域中定义的同名实体。

不连续的命名空间

命名空间可以由多个部分定义,因此命名空间由其单独定义的部分的总和组成。命名空间的各个部分可以分布在多个文件中。

因此,如果命名空间的一部分需要在另一个文件中定义的名称,则仍必须声明该名称。编写以下命名空间定义要么定义一个新命名空间,要么向现有命名空间添加新元素 -

namespace namespace_name {
   // code declarations
}

嵌套命名空间

命名空间可以嵌套,您可以在另一个命名空间中定义一个命名空间,如下所示 -

namespace namespace_name1 {
   // code declarations
   namespace namespace_name2 {
      // code declarations
   }
}

您可以使用解析运算符访问嵌套命名空间的成员,如下所示 -

// to access members of namespace_name2
using namespace namespace_name1::namespace_name2;

// to access members of namespace:name1
using namespace namespace_name1;

在上述语句中,如果您使用的是 namespace_name1,那么它将使 namespace_name2 的元素在范围内可用,如下所示 -

现场演示
#include <iostream>
using namespace std;

// first name space
namespace first_space {
   void func() {
      cout << "Inside first_space" << endl;
   }
   
   // second name space
   namespace second_space {
      void func() {
         cout << "Inside second_space" << endl;
      }
   }
}

using namespace first_space::second_space;
int main () {
   // This calls function from second name space.
   func();
   
   return 0;
}

如果我们编译并运行上面的代码,这将产生以下结果 -

Inside second_space

C语言

  1. C++ 运算符
  2. C++ 注释
  3. C++ 类模板
  4. C++ 概述
  5. C++ 常量/文字
  6. C++ 中的运算符
  7. C++ 中的数字
  8. C++ 参考资料
  9. C++ 中的命名空间
  10. C++ 模板
  11. C++ 预处理器
  12. C# - 命名空间