Latest web development tutorials

C ++ namespace

Imagine a situation when there is a class named Zara two students, in order to clearly distinguish between them, we use the name outside, had to use some additional information, such as their home address or their parents name, etc.

The same situation also appears in the C ++ application. For example, you might write a function named xyz (), in another library available there is also a same function xyz (). Thus, the compiler can not determine what you are using a xyz () function.

Therefore, the introduction of the concept ofnamespaces, designed to solve the above problems, it can be used as additional information to distinguish different libraries with the same name functions, classes, variables, and so on.Using namespace that is defined context. In essence, the namespace is the definition of a range.

Defining namespaces

Defined namespace keywordnamespace, 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 precede it with the name of the namespace, as follows:

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

Let's see how the namespace definition for the entity and other variable or function:

#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 instruction

You can use theusing namespace directive, so that when you can not use a namespace prefixed with namespace name.This directive tells the compiler that the subsequent code will use the specified namespace name.

#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

using instructions may also be used to specify the namespace specific items. For example, if you only intend to use std namespace cout section, you can use the following statement:

using std::cout;

Subsequent code when using cout you can not add the namespace name as a prefix, but thestd namespace other items still need to add the namespace name as a prefix, 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!

Nameusing instructions introduced follow the normal scope rules.Names froma using directive initially visible until the end of the range.In this case, the same name outside the scope of the definition of entities is hidden.

Discontinuous namespace

Namespace can be defined in several different parts, so the namespace is composed of several parts separately defined composition. Various components of a namespace can be spread across multiple files.

Therefore, if the namespace part of the need to request a name defined in another file, you still need to declare the name. The following namespace definition can define a new namespace, it can be for an existing namespace to add new elements:

namespace namespace_name {
   // 代码声明
}

Nested namespaces

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

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

You can use the :: operator to access the nested namespace members:

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

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

In the above statement, if you are using namespace_name1, then within the range of elements namespace_name2 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