Issue: Suppose an identifier has two different declarations, one in an outer block and one in a nested inner block. If the name is accessed within the inner block, which declaration is used?
Hypothesis: The closest definition will be used.
Proximity sounds like a good rule of thumb — whatever was done most recently matters the most.
The Experiment: When we display the value of arg1
within the inner block, following its second declaration, we can get
an answer to our question:
int main()
{
int arg1;
arg1 = -1;
...
{
char arg1 = 'A';
cout << arg1 << endl;
}
}
Question #8.9.1: If -1 is printed, what does this mean?
Question #8.9.2: If A is printed, what does this mean?
Observation: Compile and execute your program.
Question #8.9.3: What does the program display?
Conclusions:
Question #8.9.4: Is our hypothesis correct? How do you know? If it's not correct, what should it be?