C + + namespace (c + + primer Plus notes)

Source: Internet
Author: User
Tags function prototype

C + + Namespace
    1. Namespace terminology
    2. New namespace attributes
    3. Concept of use of namespaces
    1. Namespace terminology
      The names in C + + include: variables, functions, structs, enumerations, classes, and members of classes and structs.
      Namespaces: When program projects become larger, the likelihood of name collisions in C + + increases, and namespaces appear to resolve name collisions in C + +.
      Declaration area: Is the area in which the declaration can be made. For example, a global variable whose declaration area is the file in which it is declared, the local variable whose declaration area is the block of code whose declaration resides.
      Potential scope: The potential scope of a variable starts at the declaration point and ends at its declaration area. Therefore, the potential scope is smaller than the declaration area, because the variable must be defined before it can be used. However, a variable in the declaration area is not visible anywhere in the potential scope, and it may be hidden by a variable of the same name declared in the nested declaration area.
    2. New namespace attributes
      Name Space effect:
      C + + Adds a feature that creates a named namespace by defining a new declaration area, one of which is to provide a region for declaring names. A name in one namespace does not conflict with another name with the same name, while allowing other parts of the program to use that name to declare something.
      Create the Namespace keyword namespace with the following syntax:

      namespace name {int value;void func();}
  • The namespace can be global or in another namespace, but not in a code block.
  • The name declared in the namespace is externally linked unless it references a constant.
  • The global namespace corresponds to the file-level declaration area, and the global variable is in the global namespace.
  • The Declaration and definition rules in the namespace are the same as the global Declaration and definition rules.
  • Namespaces are open, and you can add names to existing namespaces. The syntax format is as follows:

    namespace name  {double value2;}

    The syntax for providing a definition for a function prototype in a namespace is as follows: (you can provide a definition later in the file or in another file):

    namespace name {void func() {....}}

    Access namespaces: Names that are not decorated are called unqualified names, and names that contain namespaces are called qualified names.
    Use the parse operator to access the name:

    name::value = 100;

    Simplifies access to namespaces with using declarations and using-compilation directives:

    using name::value;using namespace name;
  • After a using declaration adds a specific name to the declaration area to which it belongs, you can use value instead of Name::value to simplify the code.
  • The keyword using namespace makes all the names in the namespace available, without the need to use the domain resolution operator.
  • Using a using-compilation directive in the global declaration area will make the name of the namespace globally available.
  • Using domain resolution operators avoids the ambiguity of names, using declarations, and using compilation directives, which can result in ambiguous names.
  • If the namespace and the declaration area define the same name, you cannot import the namespace with a using declaration, which results in a name conflict, which the compiler does not allow. However, the namespace can be imported with a using-compiled directive, where only the local name hides the namespace name and can be accessed with the domain resolution operator.
    A using declaration is more secure than a using-compiled directive because the compiler gives instructions if a name conflicts with a local name, and the latter does not issue a warning.
    Namespace nesting

    namespace name1 {namespace name2 {    int value2;}int value1;}

    Ways to access value2:

    using name1::name2::value2;value2 = 100;using namespace name1::name2;value2 = 100;using namespace name1;   name2::value2 = 100;
  • The using compiler directives are transitive, that is, nested namespaces are imported together.
  • Create namespace Aliases: namespace Alias_name = name, which can be used to simplify the use of nested namespaces.
  • Unnamed namespaces: You can create an unassigned namespace by omitting the name of the namespace. Just as with the using compiler directive, that is, the name declared in the namespace is potentially scoped to the beginning of the declaration point to the end of the declaration area. The name in the namespace cannot be used outside of the file to which the unnamed namespace belongs, providing a substitute for a static variable that is linked internally.

3. concept of use of namespaces

    • Instead of using an external global variable, use the variable declared in the named namespace.
    • Use a variable declared in a named namespace instead of using a static global variable.
    • If you develop a function library or class library, place it in a namespace. For example, C + + advocates placing the standard libraries in the namespace Std.
    • Use only the compiled instruction using as a stopgap for converting old code to use namespaces.
    • Do not use the using compilation directives in the header file. First, this obscures what names are available and, in addition, the order in which the header files are included may affect the behavior of the program. If it is not intended to be used, it should be placed after all preprocessor compilation directive # include.
    • When you import a name, you first use the scope resolution operator or the using declaration method.
    • For a using declaration, first set its scope to local rather than global.

C + + namespace (c + + primer Plus notes)

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.