Namespaces in C++

 1. Why use namespaces

Consider a situation when we have two people with the same name, Zara, in the same class. When we need to distinguish them we must use some additional information and their names, such as this area, if they live in different areas or their mother or father's name, etc.

  The same will happen in your C ++ application. For example, you might be writing some code with a function called xyz (), and there is another library available that also has the same xyz () function. Now the compiler cannot know which version of the xyz () function you are referring to in the code.

  The namespace is designed to overcome this difficulty and is used as additional information to distinguish similar functions, classes, variables, etc., which have the same name in different libraries. Using the namespace, you can define the context in which the name is defined. Essentially, the namespace defines a scope.

Second, the definition of the namespace

There is only one global scope in C language:

1. All global identifiers in the C language share a scope
2. Possible conflicts between identifiers
C ++ proposed the concept of namespaces:

1. The namespace divides the global scope into different parts.
2. Identifiers in different namespaces can have the same name without conflict
. 3. The namespace can be nested
. 4. The global scope is also called the default namespace.

Defining a Namespace

A namespace definition begins with the keyword namespace followed by the namespace name as follows −

namespace namespace_name {
   // code declarations
}

To call the namespace-enabled version of either function or variable, prepend (::) the namespace name as follows −

To call the namespace-enabled version of a function or variable, add (: :) before the namespace name, as follows:

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

Let us see how namespace scope the entities including variable and functions −

Let us see how the namespace restricts entities including variables and functions −

#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;
}

If we compile and run above code, this would produce the following result −

Inside first_space
Inside second_space

You can also avoid using the namespace directive. Add a prefix before the namespace. This directive tells the compiler that subsequent code is using the name in the specified namespace. Therefore, the following code implies the 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;
}

If we compile and run above code, this would produce the following result −

Inside first_space

The ‘using’ directive can also be used to refer to a particular item within a namespace. For example, if the only part of the std namespace that you intend to use is cout, you can refer to it as follows −

Subsequent code can refer to cout without prepending the namespace, but other items in the stdnamespace will still need to be explicit as follows −

#include <iostream>
using std::cout;

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

If we compile and run above code, this would produce the following result −

std::endl is used with std!

The names introduced in the using directive follow normal scope rules. The name is visible from the point of using directive to the end of the scope where the directive is found. Entities with the same name in the external scope are hidden.

to sum up

Use of C ++ namespace:

Use the entire namespace: using namespace name;
Use variables in the namespace: using name :: variable
Use variables from the default namespace: :: variable

Discontinuous namespace

The name space can be divided into several parts to define, so the name space consists of the sum of its separately defined parts. The various parts of the namespace can be distributed in multiple files. Therefore, if a part of the namespace requires a name defined in another file, the name must still be declared. Write the following namespace definition, or define a new namespace, or add new elements to an existing namespace-

namespace namespace_name {
   // code declarations
}

Nested namespace can nest namespace

You can define a namespace in another namespace as follows:

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

You can access the members of the nested namespace by using the resolution operator, as follows:

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

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

In the above statement, if namespace_name1 is used, it will make the elements of scope_name2 available in the scope, as shown below:

#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;
}

If we compile and run above code, this would produce the following result −

Inside second_space

Reference from https://www.tutorialspoint.com/cplusplus/cpp_namespaces.htm

Guess you like

Origin www.cnblogs.com/xxxsans/p/12717021.html