Case Analysis of data sharing and Protection of C++
This article mainly explains "case Analysis of data sharing and Protection of C++". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. Now please follow the editor's train of thought slowly and deeply. Let's study and learn "C++ data sharing and protection case analysis"!
1. Scope
A scope is an area where an identifier is valid in the body of a program.
The scope relationship from large to small is:
Namespace scope > class scope > local scope
The valid range of identifiers is the visibility of identifiers, and the general rules for visibility are:
The identifier is declared before and used after
Identifiers with the same name cannot be declared in the same scope
Identifiers of the same name declared in different scopes that do not contain relationships do not affect each other
If an identifier with the same name is defined in more than one scope, the outer identifier is not visible in the inner layer
two。 Object lifetime
If the object lifetime is the same as the run time of the program, it has a static lifetime
The local lifetime object is born at the declaration point and ends at the end of the block in which the declaration is executed.
Objects declared in the namespace scope have a static lifetime
A static lifetime variable of the base type with no initial value specified at the time of definition is initialized with a value of 0.
If you want to declare an object with a static lifetime in the local scope within the function, use the keyword static
Void f () {static int masks 0; / / m can only access masks in f; cout