Nested blocks: beginners question.

closed account (iACGNwbp)
I'm currently trying to learn C++ from "Savitch - Absolute C++"
In Ch. 3 he gives the definition of a block:
"A block is some C++ code enclosed in braces."

And he gives the Scope Rule for Nested Blocks:
"If an identifier is declared as a variable in each of two blocks, one within the other, then these are two different variables with the same name.
One variable exists only within the inner block and cannot be accessed outside
the inner block. The other variable exists only in the outer block and cannot
be accessed in the inner block."

I thought I understood this, but then an exercise followed, asking to state the output of the following code fragment:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  {
	int x = 1;
	cout << x << endl;
	{
		cout << x << endl;
		int x = 2;
		cout << x << endl;
		{
			cout << x << endl;
			int x = 3;
			cout << x << endl;
		}
		cout << x << endl;
	}
	cout << x << endl;
}


According to the given Scope Rule, I would say that "int x = 1" is in an
outer block, "int x = 2" in an inner block of the first block, and "int x = 3" in an inner block of the second block.
So, as far as I understand, when the second cout is executed, no x has been defined (int x = 1 is in the outer block and cannot be accessed from the inner block).
But I appear to be wrong: the output of the second cout is "1", so it references
the x in the outer block.
So I'm afraid there's something I misunderstand. I hope someone on this list can
shed some light on this.
Last edited on
I'll try and word this as well as possible.

Assuming this entire function is the 'main' function of the program. int x = 1 is defined at the start. blocks of code within this main area can now use this int. The scope rule only applies in reverse/

for instance if we had

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 {
	int x = 1;
	cout << x << endl;
	{
		cout << x << endl;
		int x = 2;
                         int y = 5;
		cout << x << endl;
		{
			cout << x << endl;
			int x = 3;
			cout << x << endl;
		}
		cout << x << endl;
	}
	cout << x << endl;
          cout << y << endl;
}


That y will not be cout'd and will not compile as the y integer is only in scope of the inner block.

The same could be said for a global variable. If you define a variable before the main loop but not within a function, then this variable can be used at any point within a block of code afterwards, as long as in the case above, it doesn't get overwritten to become a local variable for a set block of code
Last edited on
There are two different concepts here.

One, a variable defined within an inner block cannot be accessed at all from an outer block.

Two, a variable previously defined in an outer block can be accessed from an inner block, except where the inner block "hides" that variable by defining another variable with the same name.
closed account (iACGNwbp)
Thanks to both posters.
I think the last reply gave the exact answer: as soon as a variable has been defined in an inner block, the variable with the same name in an outer block will be "hidden".
I missed this in the above mentioned Scope Rule for Nested Blocks..
Last edited on
Topic archived. No new replies allowed.