Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The namespace for C+


May 11, 2021 C++


Table of contents


The namespace for C+

Suppose, in a situation where there are two students in a class named Zara, in order to make a clear distinction between them, we have to use some additional information, such as their home address, or their parents' names, in addition to the names.

The same thing happens in C++ applications. F or example, you might write a function called xyz() and have the same function xyz() in another available library. In this way, the compiler cannot determine which xyz() function you are using.

Therefore, the concept of namespaces has been introduced to solve the above problems and can be used as additional information to distinguish functions, classes, variables, and so on with the same name in different libraries. T he context is defined by using a namespace. In essence, a namespace defines a scope.

Define the namespace

The definition of a namespace uses the keyword namespace, followed by the name of the namespace, as follows:

namespace namespace_name {
   // 代码声明
}

In order to call a function or variable with a namespace, you need to add the name of the namespace before it, as follows:

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

Let's look at how namespaces define ranges for entities such as variables or functions:

#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;
   }
}
int main ()
{
 
   // 调用第一个命名空间中的函数
   first_space::func();
   
   // 调用第二个命名空间中的函数
   second_space::func(); 

   return 0;
}

When the above code is compiled and executed, it produces the following results:

Inside first_space
Inside second_space

Using instructions

You can use the using namespace directive so that you don't have to add the namespace's name to the front when you use it. This instruction tells the compiler that subsequent code will use the name in 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;
   }
}
using namespace first_space;
int main ()
{
 
   // 调用第一个命名空间中的函数
   func();
   
   return 0;
}

When the above code is compiled and executed, it produces the following results:

Inside first_space

The using directive can also be used to specify specific items in the namespace. For example, if you only plan to use the cout portion of the std namespace, you can use the following statement:

using std::cout;

In the code that follows, you can use cout without having to prefix the namespace name, but other items in the std namespace still need to be prefixed with the namespace name, as follows:

#include <iostream>
using std::cout;

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

When the above code is compiled and executed, it produces the following results:

std::endl is used with std!

The names introduced by the using directive follow normal scope rules. T he name is visible from the beginning with the using instruction until the end of the range. At this point, entities of the same name defined outside the scope are hidden.

An unseth consecutive namespace

Namespaces can be defined in several different parts, so namespaces are made up of several separately defined parts. The components of a namespace can be spread across multiple files.

Therefore, if an component in the namespace needs to request a name defined in another file, you still need to declare that name. The following namespace definition can be between defining a new namespace or adding a new element to an existing namespace:

namespace namespace_name {
   // 代码声明
}

Nested namespaces

Namespaces can be nested, and you can define another namespace in one namespace, as follows:

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

You can access members in a nested namespace by using the :: operator:

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

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

In the statement above, if you are using namespace_name1, the elements in namespace_name2 in the range are also available, as follows:

#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;
      }
   }
}
using namespace first_space::second_space;
int main ()
{
 
   // 调用第二个命名空间中的函数
   func();
   
   return 0;
}

When the above code is compiled and executed, it produces the following results:

Inside second_space