C + + memory model and namespace summary __c++

Source: Internet
Author: User

C + + encourages the use of multiple files when developing programs, an effective organizational strategy is to use header files to define user types, provide function prototypes for functions that manipulate user types, and to place function definitions in a separate source code file. The header file and the source code file define and implement user-defined types and how they are used. Finally, Mian () and other good books that use these functions are placed in the third file.

C + + Storage scenarios determine how long the variable remains in memory (storage continuity) and which part of the program can access him. An automatic variable is a variable defined in a code block that only exists when the program executes to the code block that contains the definition, and is visible. An automatic variable can be declared by using the storage type specifier, auto and register, or not using a specifier at all, as with auto. The register specifier prompts the compiler that the variable is used very frequently.

Static variables exist throughout the execution of the program, and for variables defined outside the function, all functions that are in the file that are behind the definition of the variable can use it (file scope) and can be used in other files of the program (external link). For another file to use this variable, you must declare it using the extern keyword. For variables that are shared among files, you should include in a file a definition declaration (no extern) and include a reference declaration in other files (using extern). Variables defined by using the keyword static outside of a function are scoped to the entire file, but not to other files (internal link sex). Variables defined with the keyword static in a code block are limited to that code fast (local scope, no link), but all the time throughout the execution of the program, it persists and retains its original value.

By default, C + + functions are externally linked, so they can be shared among files, but the link to a function that uses a keyword static qualification is internal and is limited to the file in which it is defined.

namespace allows you to define a named range in which identifiers can be declared. This is done to reduce the name conflict, especially when the program is very large and uses the code of multiple vendors. You can make a marker in a namespace available by using the scope resolution operator, using declarations, or using compilation directives.

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.