C++ DEVELOPER SITE
PYTHONPHP7JAVA
 

C++ Namespaces


Assuming a situation where there are two students named Zara in a class, in order to clearly distinguish them, we have to use some extra information, such as their home address, or Their parents' names and so on.

The same thing happens in C++ applications. For example, you might write a function called xyz() , and there is an identical function xyz() in another available library. This way, the compiler cannot determine which xyz() function you are using.

Therefore, the concept of namespace was introduced to solve the above problem. It can be used as additional information to distinguish functions, classes, variables, etc. of the same name in different libraries. The context is defined using the namespace. Essentially, a namespace defines a scope.

We take an example in a computer system, a folder (directory) can contain multiple folders, each folder can not have the same file name, but files in different folders can be duplicated.

Define a namespace

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

namespace namespace_name { // Code Statement }

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

name::code; // code Can be a variable or function

Let's see how namespaces define scopes for entities such as variables or functions:

Example

#include <iostream> using namespace std; // First namespace namespace first_space{ void func(){ cout << "Inside first_space" << endl; } } // Second namespace namespace second_space{ void func(){ cout << "Inside second_space" << endl; } } int main () { // Call the function in the First namespace first_space::func(); // Call the function in the second namespace 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 directive

You can use the using namespace directive so that you don't have to prefix the name of the namespace when using the namespace. This directive tells the compiler that subsequent code will use the name in the specified namespace.

Example

#include <iostream> using namespace std; // First namespace namespace first_space{ void func(){ cout << "Inside first_space" << endl; } } // Second namespace namespace second_space{ void func(){ cout << "Inside second_space" << endl; } } using namespace first_space; int main () { // Call the function in the First namespace 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 a specific item in the namespace. For example, if you only intend to use the cout part of the std namespace, you can use the following statement:

using std::< /span>cout;

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

Example

#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 < /span>is used with Std!

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

Discontinuous namespace

The

namespace can be defined in several different sections, so the namespace is made up of several separately defined sections. The various components of a namespace can be spread across multiple files.

So, if a component in the namespace needs to request a name defined in another file, you still need to declare the name. The following namespace definitions can be either defining a new namespace or adding new elements to an existing namespace:

namespace namespace_name { // Code statement }

Nested namespaces

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

namespace namespace_name1 { // Code statement namespace namespace_name2 { // Code statement } }

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

// Access the member in namespace_name2 using namespace namespace_name1::namespace_name2; // access namespace:name1 Member using namespace namespace_name1;

In the above statement, if namespace_name1 is used, the elements in namespace_name2 are also available in this scope, as follows:

Example

#include <iostream> using namespace std; // First namespace namespace first_space{ void func(){ cout << "Inside first_space" << endl; } // Second namespace namespace second_space{ void func(){ cout << "Inside second_space" << endl; } } } using namespace first_space::second_space; int main () { // Call the function in the second namespace func(); return 0; }

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

Inside second_space