C ++ namespace, namespace

Source: Internet
Author: User

C ++ namespace, namespace

  Namespace)Indicates the visible range of identifier. An identifier can be defined in multiple namespaces. Its meaning in different namespaces is irrelevant. In this way, any identifiers can be defined in a new namespace and they do not conflict with any existing identifiers because all existing definitions are in other namespaces.

For example, if Bill is an employee of Company X, the employee ID is 123, John is an employee of Company Y, and the employee ID is 123. Because two people work in different companies, they can use the same employee ID to mark it without confusion. Here, each company represents an independent namespace. If the two work in the same company, their employee IDs cannot be the same, otherwise there will be confusion in the payment of wages.

This feature is the main reason for using namespaces. In large computer programs or documents, there are usually hundreds or thousands of identifiers. A namespace (or a similar method, see "namespace simulation") provides a mechanism to hide area identifiers. By organizing logical identifiers into corresponding namespaces, the entire system can be more modularized.

In programming languages, a namespace is a special scope that contains identifiers in this scope and is also represented by an identifier, in this way, a series of logically related identifiers are organized with one identifier. Many modern programming languages Support namespaces. In some programming languages (such as C ++ and Python), The namespace identifier also belongs to an outer namespace, that is, the namespace can be nested to form a namespace tree, the root is an unknown global namespace.

The scopes of functions and classes can be considered as implicit namespaces, which are associated with the visibility, accessibility, and object lifecycle.

 

Namespace in C ++

In C ++, namespace is used to declare and {} is used to define the namespace scope. For example:

namespace foo {    int bar;}

The namespace can be global or in another namespace, but not in the class and code block. Therefore, the name declared in the namespace has the external link feature by default (unless constants are declared, constants are static in a compilation unit by default ).

In addition to all namespaces, a global namespace exists, corresponding to the global scope when the namespace is not used. For example,: new is the global new operator.

The namespace can be divided into namespace and namespace based on whether there is a name. The latter is:

Namespace {namespace-body (that is, declaration sequence (optional ))}

Members of an unnamed namespace can be directly used in this compilation unit without explicit reference (in fact, the reference to an unnamed namespace cannot be displayed), but are not visible in other compilation units. Internal Link Attributes.

The namespace member is the name declared in curly brackets of the namespace. You can define a namespace member in addition to the namespace body. That is, the namespace member declarations and definitions can be separated.

The sub-namespace must be defined in the upper namespace. Do not separate sub-namespace declarations from definitions.

You cannot add a new member to a namespace outside the namespace body by using the namespace name: member name; method. You must add a new member declaration to the namespace body.

You can declare and define the same namespace multiple times and add new members to the namespace each time. The compiler automatically merges these namespaces with the same name. (In fact, the compiler does not merge a namespace or enumerate members of a namespace. The compiler only adds the name qualifier to all namespace members as their decorative name mangling name)

You can add members of other namespaces to a namespace. For example:

namespace myNameSpace{       using namespace His_NameSpace;       using OLib::List;       void my_func(String &, List &);}


To reference a namespace Member, follow these steps:

  • Use the namespace's scope parsing OPERATOR:, for example: std: cout
  • Using namespace name [: namespace name…]; This statement allows all members in the specified namespace to be directly used. If the introduced name conflicts with a local name, the compiler does not issue any warning information, but uses a local name to automatically overwrite members of the same name in the namespace.
  • Using namespace name: [namespace name:…] Name of a member. If the introduced name conflicts with a local name, the compiler reports an error.

A namespace can have an alias: namespace alias = namespace name. This allows a namespace with a long name to be easily referenced by a shorter alias.

 

Original article: namespace


Meaning of using namespace in C Language

This is the language feature that C ++ has.
If you use a library, there is a variable named abc in the library, but you accidentally define a variable named abc, which will cause a redefinition error. to avoid this problem, C ++ introduces the namespace concept. It is best to put everything in a namespace when writing a library, for example
Namespace MY
{
Int abc;
Char cab;
}
In this way, you will access the abc in MY through MY: abc in the outside world. If you define a variable named abc outside, it will not cause a redefinition error. directly calling abc will call your own abc, and only using MY: abc will actually call the abc in MY.
However, this is not too troublesome. If you make sure that you do not repeat the names in others' libraries, you can call up all the names in their namespaces. This process is using namespace, for example, enter
Using namespace MY;
In this way, when I call abc again, the abc in MY will be automatically called.

How to teach C language and C ++ namespaces

Technically speaking, there is no such thing as <iostream. h>-the Standardization Board replaced the non-C standard header file with <iostream>
It. It must be known that if the compiler supports both <iostream> and <iostream. h>, the use of the file name at the other end will be very subtle. For example,
If # include <iostream> is used, the elements of the iostream library placed under the namespace std are obtained.
Use # include <iostream. h> to obtain the same elements in the global space.
Retrieving elements in a global space results in name conflicts.
It is to avoid such name conflicts.
All content in iostream is encapsulated in the std namespace, but not in iostream. h. Therefore, if # include
<Iostream>, using namespace std is required; if it is # include
<Iostream. h>. All classes and objects defined in iostream. h are in the global space, so you can directly use cout
However, in iostream, all the things it defines are in the namespace std, so you must add using namespace std to use cout.
Generally, an old C ++ tape ". h. h. There is a ". h "extension corresponds, except for the latter
In addition to other improvements, the latter stuff is added to the "std" namespace.

Related Article

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.