The previous answers already indicated the reason, but I just want to show that this is still allowed:
void foo(){
for(int i = 0; i < 10; ++i){
int a = 1;
}
int a = 0;
}
In this case, the ainside does not hide the external a, therefore it is valid.
And IMHO should also be like this in C ++, this is less confusing and prevents accidental declaration of a variable with the same name.
source
share