C + + Learning 2-namespaces

Source: Internet
Author: User
Tags cmath

The concept of introducing namespaces (Namespace) into the C + + language is primarilyto avoid naming conflicts, and its key word is namespace.

The development of science and technology today, a system is usually not only by one person to develop the complete, different people develop the same system, inevitably there will be variable or function naming conflicts, when everyone's code test pass, no problem, the code of all people together, Problems caused by the duplicate names of variables or functions can cause some confusion, such as:

int flag = 1; The variable of Xiao Li declaration
...//intermediate interval several lines of code
BOOL flag = TRUE; Korean Declaration of variables

Note: This example is only used to interpret namespaces, not as described in corporate system development, but only by namespaces to resolve naming conflicts, and specific programming specifications can refer to Lin Rui's high-quality programming Guide.

As shown above, because of the different personal habits, Xiao Li likes to declare the INT type variable for logical judgment, while the Korean prefers to use the BOOL type variable. However, when the two declarations are placed in the same function, it is clear that the compiler will prompt the flag variable to redefine the error. This problem cannot be compiled without being processed.

You can use namespaces to resolve naming conflict issues like the above, such as:

Namespace Li
{
The variable declaration of Xiao Li
int flag = 1;
}

Namespace Han
{//Korea variable declaration
BOOL flag = TRUE;
}

Xiao Li and Xiao Han each define a namespace named after their own surname, when the flags of Xiao Li and the Korean flag are placed in the same function body, there will be no problem, of course, when using these two variables, you need to indicate which namespace is used in the flag variable.

The "::" operator is required to specify the variable used, and the "::" operator is the domain resolution operator. For example:
Li::flag = 0; Use the variable flag defined by Xiao Li
Han::flag = false; Use the variable flag defined by the Korean

We have defined two namespaces Li and Han, and in each of them declare a flag variable, using the domain resolution operator to indicate who the flag variable is defined by the flag variable, whether it is Xiao Han or Xiao Li.

In addition to using the domain resolution operator directly, a using declaration (using declaration) can also be used, for example:
Using Li::flag;
Flag = 0; Use the variable flag defined by Xiao Li
Han::flag = false; Use the variable flag defined by the Korean

The Li::flag is declared at the beginning of the code with a using, meaning that if an unspecified flag is present in a later program, the Li::flag is used, but Han::flag is still required to use the flag defined by the Korean.

A using declaration can be used not only for a variable in a namespace, but also for declaring an entire namespace, for example:
using namespace Li;
Flag = 0; Use the variable flag defined by Xiao Li
Han::flag = false; Use the variable flag defined by the Korean

If other variables are also defined in the namespace Li, they also have the effect of the flag variable, which, after the using declaration, defaults to the variables in the Li namespace if a naming conflict variable is not specified.

within a namespace, you can declare or define variables, as well as other entities that can be declared or defined outside of a namespace, such as declarations or definitions of variables, declarations or definitions of functions, TypeDef, and so on, can appear in namespaces.

Let's look at an example of a simple C + + program:
#include <iostream>
using namespace Std;

int main ()
{
cout << "Hello world!" << Endl;
return 0;
}

This is a simple C + + program Hello World example, with a using declaration namespace in the program std,using namespace std; This statement covers all identifiers in the Std namespace, which contains all the standard libraries of C + +. All variables, functions, etc. defined in the header file Iostream file are in the Std namespace, and each time you use a variable or function in iostream, you need to precede the std:: It's a very troublesome thing, For this reason, all variables or functions in STD can be declared directly with a using declaration.

If you do not use the using namespace STD; This statement, the program should look like this:

#include <iostream>

int main ()
{
std::cout<< "Hello world!" <<std::endl;
return 0;
}

This may seem like a lot of trouble, and if you omit the STD when you use a variable or function in iostream, it will cause the identifier to be defined incorrectly.

The C + + language is based on the language, which inherits all of the C-language libraries, but C + + has renamed the Standard Libraries. The standard C header file (such as MATH.H) is renamed to Cmath, removing the. h of the header file and adding C to the front. Therefore, if you want to use the MATH.H header file in C + +, you can use the following method.
#include <cmath>
using namespace std;

C + + Learning 2-namespaces

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.