문제

I've miss-write my variables names using i for two variables, one is an int and the other Uint16 like so :

for(int i = 0; i <= 5; i++) {
    for(Uint16 ch = 5; ch < 20; ++ch) {
        Uint16 i = ch;
        //Some code
        cout << i;
    }
}

Using clang x86 5.1 it does compile, but which i will be displayed ? Is their a special rule ?

도움이 되었습니까?

해결책

In an inner scope, a variable declaration will shadow any previous declaration. In your code sample the output will be the value of ch on each loop iteration.

If you were to output the value of i outside the inner for, the output would be the value of the i declared in the first for loop.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top