Namespace in C
As the scale expands, the problem of naming conflicts is getting more and more serious, and one module of the same program or even a program will appear as the same name, especially those in the program, which is more serious, in order to solve This problem, various languages have corresponding measures such as: Java use "package" concept, and C uses "namespace", because the object discussed is only for C standard template library, only discuss namespace And the concept of the package in Java is similar to namespace. If you are interested, you can refer to Java's books. All objects in the standard library are in a space called STD. If you want to use, you must introduce this space, you can introduce by the statement:
Using namespace std;
The subsequent code can use the function, algorithm, etc., as if it is called a custom function in the program, many examples of books that use the C standard template library use this way, indeed recognize this way Very simple, but there is a problem, please think about the situation below:
A slightly larger program tend to contain many functions, the names and parameter types of these functions are likely to be with the function in the standard library. If this happens, the function in your program will block the function in the standard function library. But you think it is using the function in the standard library, so I forgot to use the std :: role domain resolution, such words, you will compile, but there will be a potential error.
For example, as follows:
Suppose there is a function of a function prototype BOOL ISOK (INT X) in the standard library. It is determined if the X is the number of prime, if so, return to the false, and you also define a function of such prototypes in your program, However, its role is to determine whether the range of the number is [0 ~ 100], if it returns true, otherwise returns false. Now there are many places in your program to use the ISOK function in the standard library to determine if it is the number of prime, but you may use the ISOK function directly because "I have already introduced STD space" I have already introduced the STD Space ". Your original intention is to judge whether it is the number of prime, but it has become [0 ~ 100] in the scope of the number of judgment, so that it does not match your intentions, it is unfortunately, unfortunately the program will Through compilation, you have such a potential error.
How to solve this potential mistake, I think there are two types:
The first: use the USING NAMESPACE STD statement to directly use the function in the standard library, but put the custom functions in your program in one space, then use the custom function using the:: Scope resolution.
#include using namespace std; // introduced STD namespace
Namespace zqspace // Define your namespace
{
Bool isok (int x)
{
IF (x <0 || x> 100)
{
Return True;
}
Return False;
}
}
/ / -------------------------------------------------------------------------------------------- ---------------------------
Int main (int Argc, char * argv [])
{
INT TEMPNUM = 133;
IF (zqspace :: ISOK (TEMPNUM)) // Using the Scope Results Use your own defined function
{
COUT << Tempnum << "" in the range of [0 ~ 100]! "<< endl;
}
Else
{
COUT << Tempnum << "Not in the range of [0 ~ 100]!" << endl;}
}
The second: use the USING NAMESPACE ZQSAPCE statement and use the function in your namespace, if you use the function in the standard library, use:: Scope resolution, the following statement is the use of the Sort algorithm in the standard template library to student results put in order.
// Use the Sort Algorithm in the Standard Template Library
Std :: Sort (Finalgrade.begin (), Finalgrade.end (), gradeCompare ());
Note: Comparison results (gradeCompare ()) object and the definition of vector representation of student grades, please refer to the use of the function object.