![]() |
#2
zhoufeng19882010-08-19 10:35
|

//作用域问题
#include <iostream>
using namespace std;
int main( )
{
int count1 = 10;
int count3 = 50;
cout << endl
<< "Value of outer count1 = " << count1
<< endl;
{
int count1 = 20; //此为内部变量 count1
int count2 = 30; //此为内部变量 count2
cout << "Value of inner count1 = " << count1
<< endl;
count1 += 3; //此处的运算是内部运算
cout << "My count1 = " << count1 << endl; //输出内部运算的值 23
count3 += count2; //此 count3 是在外部定义的,属全局变量,count 2 为局部变量
}
cout << "Value of outer count1 = " << count1 //此处输出的仍然是全局变量的 count1
<< endl
<<"Value of outer count3 = " << count3 //此处输出的是全局变量的 count3 已经在原始值上加了 30
<< endl;
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int main( )
{
int count1 = 10;
int count3 = 50;
cout << endl
<< "Value of outer count1 = " << count1
<< endl;
{
int count1 = 20; //此为内部变量 count1
int count2 = 30; //此为内部变量 count2
cout << "Value of inner count1 = " << count1
<< endl;
count1 += 3; //此处的运算是内部运算
cout << "My count1 = " << count1 << endl; //输出内部运算的值 23
count3 += count2; //此 count3 是在外部定义的,属全局变量,count 2 为局部变量
}
cout << "Value of outer count1 = " << count1 //此处输出的仍然是全局变量的 count1
<< endl
<<"Value of outer count3 = " << count3 //此处输出的是全局变量的 count3 已经在原始值上加了 30
<< endl;
system("pause");
return 0;
}