Deep understanding of namespaces

Source: Internet
Author: User

First, why use namespaces

A large project is often done independently by a number of people, different people complete different parts, and finally combined into a complete program. Since each header file is designed by different people, it is possible to name the defined class or function in different headers with the same name, so that there is a naming conflict in the program. Not only that, it is possible that our own defined names will conflict with names in the C + + library.

The name conflict is that there are two or more entities with the same name in the same scope, in order to resolve the naming conflict, the namespace is introduced in C + +, the so-called namespace is a scope that can be defined by the user, and the variables of the same name can be defined in different scopes, and the system can distinguish them.


Ii. what is a namespace

A namespace is a memory area named by the program designer, which is separated from other global entities by specifying a number of named spatial domains and storing some global entities in each namespace, respectively.

In layman's terms, each namespace is a namespace domain, and global entities stored in the namespace domain are valid only within the domain. namespaces restrict the domain of global entities to resolve naming conflicts reasonably.

Cases:

Namespace NA

{

int A;

char c;

}

Then A and C are valid only within the scope of NA.


When declaring a namespace, you can include not only variables in the curly brackets, but also the following types:

Variable (can be initialized)

Constant

function (can be defined or declared)

Structural body

Class

Template

Namespaces (namespaces can be defined in a nested definition)

Cases:

Namespacens1

{

int A;

char C = 10;

void Fun ();

int Add (int a, int b)

{

return a + B;

}

Namespace NS2

{

int b;

}

}


Note that if you want to output B at this point, you need to specify the outer and inner namespaces

cout<<ns1::ns2::b<<endl;


Iii. methods for using namespace members

1. When referencing a namespace member, namespace members are qualified with the namespace name and scope resolver to distinguish between the same name identifiers in different namespaces. Namespace name:: namespace member name


2. You can also alias a namespace

Cases:

Namespace Television

{...}

namespace Tv=television;

The TV is the alias of the namespace television, which can be replaced with TV in the future where television is used.


3. Using a using namespace member name

The namespace member name after the using must be a name with a namespace limit.

Example: Using Ns1::a;

The above statement indicates that if you use member A in ns1 within the scope of the using, you do not have to use namespace qualification.  If a is used in a later program, the implied reference is ns1::a. The scope of the using declaration begins with the using statement and ends at the scope where the using is located. It is important to note that a member of a different namespace that is declared with a using in the same scope cannot have a member with the same name, or a redefinition occurs.


4. Use the Using namespace namespace name

The using namespace member name, described in article III, can only declare one namespace member at a time, and if there are multiple variables in a namespace, you need to use the using namespace member name multiple times. The using namespace statement is also provided in C + + to implement declaring all members of a namespace at once.

For example:

using namespace ns1;

Declares that a member in the namespace ns1 is to be used in this scope, and no namespace qualification is necessary when using any member within that namespace. In scopes declared with using namespace, the members of the namespace ns1 appear to be declared in the global domain, so no naming qualification is used.


4. Nameless namespaces

Cases:

Namespace

{

void Fun ();

}


Because there is no name, other files cannot be referenced, it can only be valid within the scope of this file, and its scope causes the renaming of the nameless namespace declaration to begin at the end of this file. You do not have to use namespace qualification when using nameless namespace members in this file. In fact, the nameless namespace and static is the same reason, are only valid in this file, can not be referenced by other files.


5. Standard namespaces

All identifiers for the C + + library are defined in a namespace called STD, or the functions, classes, objects, and class templates in the standard header file (iostream) are defined in the namespace Std. So at the beginning of a C + + program, the STD is declared globally using the using namespace.


This article from the "11132019" blog, reproduced please contact the author!

In-depth understanding of 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.