assuming the following simple code:
for(int i=0; i < 1000; i++)
{
cout << "Outer i: " << i << endl;
for(int i=0; i < 12; i++)
{
cout << "Inner i:" << i << endl;
}
}
Works very nice. The same variable name in both loops used and the output is fine.
Do I understand it right that both variables are created on stack, and when the outer loop comes to the new inner loop, a new "namespace" (maybe the wrong name..) is created? But why is it overwritten? If I choose another name for the variable in inner loop I can also access the i
from outer loop.
A bit confused I am.
See Question&Answers more detail:os