局部变量只在它所在的块中可见,内部快也是,但内部块又是特殊的块:在外部块中存在的变量名,内部块可以重复使用,且可定义成其它的实体(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:\n";
cout << "x: " << x << ‘\n‘;
cout << "y: " << y << ‘\n‘;
}
cout << "outer block:\n";
cout << "x: " << x << ‘\n‘;
cout << "y: " << y << ‘\n‘;
return 0;
}
输出:
inner block:
x: 100
y: 50
outer block :
x: 1
y: 50
内部块-inner blocks ,中的变量,布布扣,bubuko.com
原文:http://www.cnblogs.com/guozqzzu/p/3586222.html