C++primer Note (2):: namespace

Source: Internet
Author: User
Tags ming

Large programs are generally divided into multiple modules, which are developed by the collaboration of many people, which will inevitably be used in libraries. And the code of each module and the library will define a large number of variables, and a large number of variable names, will inevitably meet the "duplicate name" problem. The "duplicate name" Situation we call namespace pollution. Just like your classmates have the same name (there is no way to do things), such as the same class with two Li Ming (which is very high), this time when you mention one of them, the listener can not understand exactly what is said to be the Lee clear, this time there is a namespace pollution. At this point, the namespace comes in handy, and the so-called namespace is actually the equivalent of giving the scope a name, and then you can make various declarations and definitions in that scope. The namespace is defined as follows:

1 namespace my_namespace {    // keyword namespace   namespace name 2     int my_ Test;            //       Various declarations and definitions     3     namespace/*.... */ // nested namespaces 4 };

As long as the declarations that can be in the global scope can be placed in the namespace, including: classes, variables (and their initialization), functions (and their definitions), templates, and other namespaces.

the namespace can be discontinuous. we can open a namespace that has already been defined and add a new declaration and definition to it.

namespace my_namespace {    int my_index;              // open a namespace that has already been defined } // Note There are no semicolons here!!! 

It is important to note that in general, do not place the # include <***> in the namespace , which is tantamount to trying to nest the namespaces in the header file in our defined namespaces, and the program is generally error-prone, so it is recommended to avoid this situation.

The members of the namespace are used normally in the namespace, and prefixes are required outside the namespace. For example, two Li Ming a home is Taiyuan, Shanxi, another home is Shaanxi Xi ' An, this time you can say: Shaanxi Xi ' an Li Ming how, so that others will not be confused about the circumstances of the object.

5;

But there are so many variables in a large project that adding a prefix to each adds a huge amount of unnecessary hassle. Therefore, C + + provides namespace aliases ,using declarations , and using directives to resolve this problem.

The namespace alias is actually the same as the type alias, which allows us to set a short, multiple synonym for our defined namespace, which can alleviate a bit of work. For example, you think that every time you add that Shaanxi Xi ' an feel tired, then you can tell the listener, said: This is too tired, after I directly said that Shaanxi Li Ming, so good point.

namespace My_ns = My_namespace;

A namespace alias can also point to a nested space.

namespace Son_ns = My_namespace::son_namespace;

The using declaration acts in the same way as other declarations, and a using declaration statement introduces only one member of the namespace at a time, and its valid range is from the beginning of the declaration to the end of the scope where the declaration resides. Go back to that example and tell the listener before the conversation: I declare that I am talking about Li Ming in Xi ' An, Shaanxi, and then in this conversation you say that Li Ming he naturally knows which one you are talking about.

1 using my_namespace::my_test; 2 int 3 Main (void)4{5     ; 6 }

The using indicates that all members of the namespace are rendered to the current scope. For example, tell the listener: The people I'm talking about are all in Xian, Shaanxi province.

1 using My_namespace; 2 3 int 4 Main (void)5{6     ; 7      - ; 8 }

I personally recommend using the use directive with caution because, unlike the using declaration, we cannot control the visibility of members because all are visible and namespace contamination occurs if the two using-indicated namespaces have members of the same name.

Template specializations must define the namespace to which the original template belongs.

The global namespace is declared implicitly, and the definition in the global scope is defined in the global namespace.

// represents a member of the global namespace

The inline namespace (c++11 new standard), which is unique in that the name in the inline namespace can be used directly by the outer namespace without adding a prefix.

namespace Test {    /* This namespace is an inline namespace */};

This feature is very useful in situations where the code version is upgraded and needs to retain the old version code.

namespace Work {    namespace  edition1 {        /* Initial version Code */    };     namespace  edition2 {        /* new version Code */    }  ;};

When we update the code and the new code to fail the emergency retracement version, only need to delete and remove the inline, as long as the interface is not changed, you can achieve seamless replacement.

Unnamed namespaces are namespaces that do not have their own defined names after namespace.

namespace {    /* This is an unnamed namespace */};

Variables defined in unnamed namespaces have a static life cycle, which is created before the first use and is destroyed when the program ends.

Its function is consistent with the static in C language.

C++primer Note (2):: namespace

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.