C ++ Primer notes (2): namespace, primer namespace

Source: Internet
Author: User

C ++ Primer notes (2): namespace, primer namespace

Large programs are generally divided into multiple modules, which are developed by collaboration among multiple people, and libraries are inevitably used. The Code of each module and the library define a large number of variables, while the naming of a large number of variables inevitably leads to the "Duplicate name" problem. In the case of "duplicate names", we call it namespace pollution. Just as if your classmates have duplicate names (this is also impossible), for example, there are two li ming in the same class (this name has a very high duplicate rate). When you mention one of them, the Listener does not know which Li Ming he is talking about. At this time, namespace pollution occurs. At this time, the namespace will come in handy, and the so-called namespace is actually equivalent to a name for the scope, and then you can make various declarations and definitions in this scope. The namespace is defined as follows:

1 namespace my_namespace {// keyword namespace name 2 int my_test; // various declarations and Definitions 3 namespace son_namespace {/*.... * // nested namespace 4 };

Declarations that can be made within the global scope can be placed in namespaces, including classes, variables (and their initialization), functions (and definitions), templates, and other namespaces.

The namespace can be discontinuous. We can open a defined namespace and add new declarations and definitions to it.

Namespace my_namespace {int my_index; // open a defined namespace} // note that there is no semicolon here !!!

It is worth noting that # include <***> should not be placed in the namespace normally, this means that we try to nest the namespace in the header file in the namespace we define, and the program will generally encounter errors, so we recommend that you avoid this situation.

The namespace member can be used normally in the namespace, And the prefix must be added outside the namespace. For example, if two li ming families are located in Taiyuan, Shanxi Province and the other is located in Xi'an, Shaanxi Province, you can say: How does Li Ming from Xi'an, Shaanxi province do, in this way, other people will not be confused about objects.

my_namespace::my_test = 5;

However, there are many variables in a large project. Therefore, adding a prefix to each project will cause a huge amount of unnecessary trouble. Therefore, C ++ provides namespace aliases, using declarations, and using instructions to solve this problem.

Namespace aliasIn fact, it is the same as the type alias. It allows us to set a short synonym for the namespace we define, which can reduce the workload. For example, you may say that Xi 'an, Shaanxi Province, is tired every time. Then you can tell the listener, saying, "This is too tired. I will directly talk about Li Ming in Shaanxi province later. This is better.

namespace my_ns = my_namespace;

A namespace alias can also point to a nested space.

namespace son_ns = my_namespace::son_namespace;

Using statementIs consistent with other declarations. A using declaration statement only introduces one member of the namespace at a time, and its effective scope starts from the declaration to the end of the scope of the declaration. Back to the example above, I told the listener before this talk: I declare that I am talking about Li Ming from Xi'an, Shaanxi province, then in this conversation, you said that Li Ming naturally knows which one you are talking about.

1 using my_namespace::my_test;2 int3 main(void)4 {5     my_test = 10;6 }

Using indicationIt is to present all the members of the namespace to the current scope. For example, I will tell the listener that all the people I will talk about next are from Xi'an, Shaanxi province.

1 using my_namespace;2 3 int4 main(void)5 {6     my_test = 10;7     my_index = 100;8 }

I personally suggest using the using instruction with caution. Because it is different from the using statement, we cannot control whether the Members are visible or not, because all the Members are visible, if the two using namespaces have members with the same name, namespace pollution may occur again.

 Special TemplateIt must be defined in the namespace to which the original template belongs.

Global namespaceIt is declared in an implicit way, and the definitions in the global scope are also defined in the global namespace.

: Someone; // indicates a member in the global namespace.

Inline namespace (New C ++ 11 standard ),It is special that the name in the inline namespace can be directly used by the outer namespace without adding a prefix.

Inline namespace test {/* This namespace is an inline namespace */};

This feature is very useful when the code version is upgraded and the old version of the code needs to be retained.

Namespace work {namespace edition1 {/* initial version Code */}; inline namespace edition2 {/* New Version Code */};};

When we update the Code and the new Code has a fault and urgently withdraw the version, we only need to add or delete the inline. As long as the interface remains unchanged, we can achieve seamless replacement.

Untitled namespace,It refers to a namespace without its own name.

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

The variables defined in the namespace that are not named have a static life cycle. They are created before they are used for the first time and are destroyed only when the program ends.

The function is consistent with the static function in C.

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.