[Base] C ++: the name of the scope

Each space has its own name, the same name in different scopes may point to different entities.

  • Scope: {} is usually separated.

  • Effective area began with the statement name name to the end where the declaration is completed.

#include<iostream>
using namespace std;
int main() { int i = 10; for (int j = 0;j <= 10;j++) { } } 
  • main braces defined before, with global scope (global scope).
  • i is defined within the scope of the main functions, from the beginning until the end of the main function i can access it, but the main function can not be visited.
  • j in the for loop, a for loop will not be able to access j.

Nested scope

  • Scope is included called 内层作用域(inner scope).

  • Contains other scopes scope called 外部作用域(outer scope).

  • Allowing the inner scope to redefine the existing outer scope name.

#include<iostream>
using namespace std;
int a = 10; int main() { cout << a << endl; // (1)输出10 int a = 0; cout << a << endl; // (2)输出0 cout << ::a << endl; // (3)输出10 system("pause"); return 0; } 
  • (1) output 10, as a defined in the global domain.
  • (2) the output occurs after a re-definition of local variables, then the output current of a redefined scope.
  • (3) the output of global scope defined by a (scope is left empty, the request occurs global scope).

Transfer from stray marmot blog, the original link: https: //www.jianshu.com/p/b768a90568b4

Guess you like

Origin www.cnblogs.com/damaohai/p/11497214.html