C + + namespace interpretation

Source: Internet
Author: User

using Key Words

If you need to refer to a member of a namespace more than once in a program, it is cumbersome to use the scope resolver to specify the namespace each time, as we have previously said. To solve this problem, the Using keyword has been introduced. Using statements are typically used in two ways:

  using namespace namespace name;

Frees the entire namespace.

  Using namespace name:: member;

Release a member of a namespace

For example:

  using namespace Std;

using namespace Std::cin;

Cause:

The purpose of using namespaces is to localize the names of identifiers to avoid naming conflicts. In C + +, variables, functions, and classes exist in large numbers. Without namespaces, the names of these variables, functions, and classes will all exist in the global namespace, causing many conflicts. For example, if we define a function toupper () in our own program, this overrides the ToUpper () function in the standard library, because both functions are in the global namespace. Naming conflicts can also occur in cases where a program uses two or more third-party libraries. At this point, it is quite possible that the name in one of the libraries is the same as the name in the other library, which is a conflict. This situation often occurs on the name of the class. For example, we define a stack class in our own program, and a library in our program may have a class with the same name, and the name conflicts.

The advent of the Namespace keyword is aimed at this problem. Because this mechanism is localized for the names declared therein, the same name can be used in different contexts without causing a name conflict. Perhaps the biggest beneficiaries of namespaces are the standard libraries in C + +. Before a namespace occurs, the entire C + + library is defined in the global namespace (which is, of course, the only namespace). With the introduction of namespaces, the C + + library is defined in its own namespace, called Std. This reduces the likelihood of name collisions. We can also create our own namespaces in our own programs so that we can localize names that we think might cause conflicts. This is especially important when we are creating classes or libraries of functions.

Namespace Basics

The namespace keyword allows us to separate the global namespace by creating a scope. Essentially, a namespace defines a scope. The basic form of defining the namespace is as follows:

namespace name {//content}

namespacecounternamespace{intUpperbound; intlowerbound; classCounter {intcount;  Public: Counter (intN) {if(N <=upperbound)
{Count=N; } Else
{Count=Upperbound; } } };}

However, since namespaces define a scope, we need to use the scope resolution operator to refer to the objects in the namespace outside of the namespace. For example, assigning a value of 10 to a upperbound outside the scope defined by the namespace Counternamespace must be written like this:

Counternamespace::upperbound = 10;

Or the object that you want to declare a counter class outside the scope of the Counternamespace definition must be written like this:

Counternamespace::counter obj;

In general, a member who wants to access the inside of a namespace outside the namespace needs to precede the member with the namespace and scope resolution operators.

C + + namespace interpretation

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.