C ++ namespace

Source: Internet
Author: User

In C ++, a name can be a symbolic constant, variable, Macro, function, structure, enumeration, class, and object. To avoid conflicts between the names of these identifiers during large-scale program design and when programmers use a variety of C ++ libraries, standard C ++ introduces the keyword namespace (namespace/domain) to better control the scope of the identifier.

 

1. Scope and namespace

Concepts related to namespaces include:

Declaration region: the region where the identifier is declared. For example, if a global variable is declared outside the function, its declaration domain is the file where the declaration is located. The Declaration field of a local variable declared in a function is the code block where the declaration is located (for example, the entire function body or the entire compound statement ).

Potential scope: the area from the declaration point to the end of the Declaration domain. Because c ++ adopts the principle of first declaring and then using, the identifier cannot be used in the Declaration domain before the declaration point. That is, the potential scope of the identifier is generally smaller than its declared domain.

Scope -- the scope of the identifier visible to the program. The identifier is not visible anywhere in its potential scope. For example, local variables can shield global variables, and inner variables in the nested hierarchy can shield outer variables, so that the blocked global or outer variables are invisible in the area where they are times shielded. Therefore, the scope of an identifier may be smaller than its potential scope.

A namespace is a mechanism used to describe logical groups. You can put declarations that belong to the same group logically according to certain standards in the same namespace.

The original C ++ identifier has three levels of scopes: code blocks ({......}, Such as composite statement and function body), class, and global. Now, the standard C ++ has added the namespace scope level between the class and the global.

A namespace can be global or in another namespace, but not in a class or code block. Therefore, the name (identifier) declared in the namespace has the external link feature by default (unless it references a constant ).

In addition to all namespaces, a global namespace exists, which corresponds to a file-level declaration domain. Therefore, in the namespace mechanism, the original global variables are now considered to be in the global namespace.

All content contained in the Standard C ++ Library (excluding the Standard C library) (including constants, variables, structures, classes, and functions) are defined in the namespace STD (Standard standard.

 

2. Define a namespace

There are two forms of namespaces: famous and unknown.

The namespace Definition Format is as follows: (from the C ++ standard documentation)

Named-namespace-definition:

Namespace identifier {namespace-body}

Unnamed-namespace-definition:

Namespace {namespace-body}

Namespace-body:

Declaration-seqopt

 

(Translated and rewritten by yourself)

Famous namespace:

Namespace name {

Declaration Sequence

}

Unnamed namespace:

Namespace {

Declaration Sequence

}

 

The namespace member is the name declared in curly brackets in the namespace definition. You can define the namespace member (internal definition) within the namespace definition ). You can also declare a member only within the namespace definition. In addition to the namespace definition, you can define the member (external definition) of the namespace ).

The namespace member's external Definition Format is:

Namespace name: member name ......

For example:

// Out. h

Namespace outer {// namespace outer Definition

Int I; // internal definition of namespace outer Member I

Namespace inner {// internal definition of the sub-namespace inner

Void F () {I ++;} // internal definition of namespace inner member F (), where I is outer: I

Int I;

Void g () {I ++;} // internal definition of namespace inner member g (), where I is inner: I

Void H (); // namespace inner member H () Declaration

}

Void F (); // namespace outer member F () Declaration

// Namespace inner2; // error. The sub-namespace cannot be declared.

}

Void outer: F () {I --;} // external DEFINITION OF THE namespace outer member F ()

Void outer: inner: H () {I --;} // external definition of namespace inner member H ()

// Namespace outer: inner2 {/*...... * // Error. The sub-namespace cannot be defined externally.

 

Note:

You cannot declare (another nested) Sub-namespaces in The namespace definition. You can only define sub-namespaces in The namespace definition.

You cannot directly use "namespace name: member name ......" To add a new member to a namespace, you must first Add a new member declaration to the namespace definition.

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.