Skip to content

Wiki

Namespace

About 4 min read

1.Namespaces and Name Conflicts

In the C++ application. For example, you might write a function named xyz(), and an identical function xyz() also exists in another available library. In this case, the compiler cannot determine which xyz() function you are using.

Therefore, the concept of namespace was introduced specifically to solve the above problem. It can serve as additional information to distinguish functions, classes, variables, etc. with the same name in different libraries. Using a namespace defines a context. Essentially, a namespace defines a scope.

1.1.Define Namespace

A namespace is defined using the keyword namespace, followed by the name of the namespace, as shown below:

namespace namespace_name {
   // 代码声明
}

Run result: This code block does not contain the main function; it is a syntax, interface, or multi-file example and cannot be run independently, so there is no terminal output.

To call a function or variable with a namespace, you need to prefix it with the namespace name, as shown below:

name::code;  // code 可以是变量或函数

Run result: This code block does not contain the main function; it is a syntax, interface, or multi-file example and cannot be run independently, so there is no terminal output.

1.2.using directive

You can use the using namespace directive, so that when using a namespace you do not need to prefix it with the namespace name. This directive tells the compiler that subsequent code will use names from the specified namespace.

#include <iostream>
using namespace std;

// 第一个命名空间
namespace first_space{
   void func(){
      cout << "Inside first_space" << endl;
   }
}
// 第二个命名空间
namespace second_space{
   void func(){
      cout << "Inside second_space" << endl;
   }
}

// 第三个命名空间
namespace third_space{
   void func()
   {
      cout << "Inside second_space" << endl;
   }
}

using namespace first_space;
using second_space::func;    //(与25行代码建议不能共存,因为都导入了func()函数),(这种方法更好更推荐)

int main ()
{

   // 调用第一个命名空间中的函数
   func();
   // 调用第二个命名空间中的函数   
   func();         //(与32行代码不能共存)
   //调用第三个命名空间中的函数
   third_space::func();

   return 0;
}

Run result: Compilation failed:using namespace Introduce two items with the same name func() into the current scope at the same time; when calling func(), ambiguity arises.

1.3.Nested namespaces

Namespaces can be nested; you can define one namespace inside another namespace, as shown below:

namespace namespace_name1 {
   // 代码声明
   namespace namespace_name2 {
      // 代码声明
   }
}

Run result: This code block does not contain the main function; it is a syntax, interface, or multi-file example and cannot be run independently, so there is no terminal output.

You can access members in nested namespaces by using the :: operator:

// 访问 namespace_name2 中的成员
using namespace namespace_name1::namespace_name2;

// 访问 namespace_name1 中的成员
using namespace namespace_name1;

Run result: This code block does not contain the main function; it is a syntax, interface, or multi-file example and cannot be run independently, so there is no terminal output.