Scope
The most basic part of any language is the variable, and the variable has two very important characteristics, scope and lifetime.
Defined
A scope is a property of a variable, and a variable is scoped to that variable in its valid area in the code.
Block Scope
Start at the variable definition and end at block. (If a nested block is to be defined as the case), the scope of the function parameter is similar. However, here is a novel function, first encountered ,Function-try-block.
int f(int n = 2) // scope of ‘n‘ beginstry // function try block{ // the body of the function begins ++n; // ‘n‘ is in scope and refers to the function parameter { int n = 2; // scope of the local variable ‘n‘ begins // scope of function parameter ‘n‘ interrupted ++n; // ‘n‘ refers to the local variable in this block } // scope of the local variable ‘n‘ ends // scope of function parameter ‘n‘ resumes} catch(...) { ++n; // n is in scope and refers to the function parameter throw;} // last exception handler ends, scope of function parameter ‘n‘ ends
For the first time, always feel that the code is wrong. In fact, the code is completely correct. So what is the use of such a function?? I don't know.
Function prototype scope
The function declaration parameter starts at the argument declaration and ends with the function declaration.
const int n=10;int f(int n,int m = n); //error,n referrence to (int n) not (const int n)
function scope
The variables in the function belong to block scope, and the labels in the function are not the same scope as the variables.
void f(){ goto aaa; // 从函数开始,即是作用域aaa: goto aaa; //直到函数结束}void g(){ goto aaa; //error, 只在label声明的函数中,为其作用域,其他函数不是其作用域}goto aaa; //error
The label is scoped to all locations in the function, either before or after the declaration.
namespace scope
In the source program, in fact, the global scope is also a namespace, the scope of the variables in the global namespace from the beginning of the Declaration, to the compilation unit break, at the time of the connection, but also the multiple compilation unit of the global namespace joined together.
The scope of a variable for the namespace:
- From the beginning of the declaration, to the end of the namespace break, start with a new namespace with the same name.
- Where using is used, the part is added to the scope.
- The namespace's child namespace is also included.
The scope of the anonymous namespace and the inline namespace variable contains its father namespace.
namespace A{ //Scope of A begins int a1=3; //Scope of a1 begins inline namespace B{ //Scope of B begins int a2; //Scope of a2 begins } namespace C{ //Scope of C begins int a3=3; //scope of a3 begins int f(){ cout<<a1<<endl; } } //scope of a3 ends namespace { int a4; //scope of a4 begins } //int a2; //error: duplicate defination a2} //scope of a1,a2,B,C,a4 interruptednamespace A{ //scope of a1,a2,B,C,a4 continue}
Class scope
The scope of variables in class is:
- Start with a declaration, end with class
- In the function body of the member function in all classes, either before the declaration or outside the class
- In the default parameters of the function
- All members nested within a function
Cases:
class X { int f(int a = n) { // X::n is in scope inside default parameter return a*n; // X::n is in scope inside function body } int g(); int i = n*2; // X::n is in scope inside initializer// int x[n]; // Error: n is not in scope in class body static const int n = 1; int x[n]; // OK: n is now in scope in class body};int X::g() { return n; } // X::n is in scope in out-of-class member function body
There are four ways to access members in a class:
- In the scope of class, or in the scope of a subclass
- Class or subclass object using '. '
- Pointer to class or subclass object, using '--'
- class or subclass use ':: '
Enumeration scope
There are two types of enumerations: Scoped enumeration and Unscoped enumeration
The scopes of the two types are different.
Scoped Enumeration: Scope starts with the variable declaration until the end of the enumeration.
Unscoped Enumeration: The scope starts with the variable declaration, ends at enumeration, and continues until the global scope ends.
enum e1{ A, B};enum class e2{ A2, B2};e1 o1 = B;//e2 o2 = B2; //error : B2 not in scopee2 o2 = e2::B2;
Where does the declaration take place
In the various scopes we mentioned above, many variable scopes start with the variable declaration point, and the variable declaration point is where.
Common variables
The declaration point precedes the initialization of the identifier after it appears:
int a=10;{ int a = a; // a is not definite}const int x = 2; // scope of the first ‘x‘ begins{ int x[x] = {}; // scope of the second x begins before the initializer (= {}) // but after the declarator (x[x]). Within the declarator, the outer // ‘x‘ is still in scope. This declares an array of 2 int.}
class, enumerate, template
The declaration occurs immediately after the identifier appears.
Type alias or alias template
The declaration occurs after alias has occurred.
using T = int; // point of declaration of T is at the semicolonusing T = T; // same as T = int
Enumerator
The declaration is after the enumerator definition.
const int x = 12;{ enum { x = x + 1, // point of declaration is at the comma, x is initialized to 13 y = x + 1 // the enumerator x is now in scope, y is initialized to 14 };}
C + + lifetime: Survival time
C + + Scopes