Wu Yuxiong - born natural C ++ language study notes: C ++ namespaces

Assume such a case, when a class has two students named Zara, in order to clearly distinguish between them, in addition to the use of the name, had to use some additional information, such as their home address, or their parents' names and many more.
The same situation also appears in the C ++ application. Function example, you might write a named xyz (), there are also a same function xyz () available in another library. Thus, the compiler can not determine which one is used XYZ () function.
Thus, the introduction of the concept of namespaces, designed to solve the above problem, it may differentiate the same library function names, classes, variables and the like as additional information. I.e. using namespace defined context. In essence, the namespace is the definition of a range.
For example, a computer system, a folder (directory) may comprise a plurality of folders, each folder can have the same file name, but different file folders can be the same name.
Defining namespaces
Defined keywords namespace namespace , followed by the name of the namespace, as follows:
 namespace namespace_name {
    // code declares 
}
To call a function or variable name with a space, it is necessary preceded by the name of the namespace, as follows:
:: code name;   // code can be a variable or function
How namespace for entities such as the definition of a variable or function:
#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 ()
{
 
   // function call first namespace 
   first_space :: func ();
   
   // call the function a second namespace 
   second_space :: func ();
 
   return 0;
}
You can use a using  namespace directive, so that when you can not use a namespace preceded by the name of the namespace. This command tells the compiler that the subsequent code will use the name specified namespace.
#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 ()
{
 
   // calls the first function namespace 
   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 a particular item namespace.
a using std :: cout;
Subsequently code when using cout you can not add the namespace name as a prefix, but the std namespace other items still need to add the namespace name as a prefix, as shown below:
#include <iostream>
using std::cout;
 
int main ()
{
 
   cout << "std::endl is used with std!" << std::endl;
   
   return 0;
}
using the name of instruction introduced follow the normal scope rules. From the use of the name using the start command is visible until the end of the range. In this case, outside the scope of the same name defined entity is hidden.
Discontinuous namespace
Namespaces can be defined in several different sections, so namespace is defined by several separate portions thereof. A namespace various components can be dispersed in a plurality of files.
So, if a part needs to request namespace name defined in another file, you still need to declare the name. The following namespace definition can define a new namespace, you can also add new elements to an existing namespace:
namespace namespace_name {
    // code declares 
}
Nested namespaces
Can be nested namespaces, namespace you can define another in a namespace, as follows:
namespace namespace_name1 {
    // code declares 
   namespace namespace_name2 {
       // code declares 
   }
}
:: operator can use to access members nested namespace:
 // access namespace_name2 members in 
the using  namespace namespace_name1 :: namespace_name2;
 
// access the namespace: name1 members in 
a using  namespace namespace_name1;
In the above statement, if the namespace_name1, then within the range of elements namespace_name2 also available, as follows:
#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 ()
{
 
   // calls the second function namespace 
   func ();
   
   return 0;
}

 

Guess you like

Origin www.cnblogs.com/tszr/p/12149995.html