65,209
社区成员
发帖
与我相关
我的任务
分享#include "stdafx.h"
#include<iostream>
using std::cout;
using std::endl;
int main()
{
int count1=10;
int count3=50;
cout<<endl
<<"value of outer count1="<<count1
<<endl;
{
int count1=20;
int count2=30;
cout<<"value of inner count1="<<count1
<<endl;
count1 += 3;
count3 += count2;
}
cout<<"value of outer count1="<<count1
<<endl
<<"value of outer count3="<<count3
<<endl;
return 0;
}
这段代码明明给count1进行了自加运算,为什么输出的值还是10,而count3的值却改变了呢。int count1=10;这段代码定义了变量count1,而后面又接着int count1=20;
[/quote]正确,也就是说编译器会以“就近”原则来处理同名问题,在局部变量的作用域中操作的只是局部的那个,除了这个作用域后的才是那个全局的
int count1
为什么没有报错重复定义