内部块-inner blocks ,中的变量

局部变量只在它所在的块中可见,内部块也是,但内部块又是特殊的块:在外部块中存在的变量名,内部块可以重复使用,且可定义成其它的实体(entity)。这个变量从内部块出来后,又恢复成原始的实体。

例如:

//#include <iostream>
using namespace std;

int main () {
  int x = 1;
  int y = 3;
  {
    int x;   //ok,inner scope
x = 100; //set value to inner x
y = 50; //set value to outer y
cout << "inner block: "; cout << "x: " << x << ' '; cout << "y: " << y << ' '; } cout << "outer block: ";
cout << "x: " << x << ' '; cout << "y: " << y << ' '; return 0; }

输出:
inner block:

                             x: 100

                              y: 50

                              outer block :

                              x: 1

                              y: 50



原文地址:https://www.cnblogs.com/guozqzzu/p/3586222.html