|
|
|
|
|
|
|
the While-loop body labeled Block3, declared within function Block2, contains its own local variable declarations. |
|
|
|
|
|
|
|
|
// ScopeRules program
#include <iostream.h>
void Block1( int, char& );
void Block2();
int a1; // One global variable
char a2; // Another global variable
int main()
{
.
.
.
}
//******************************************************************
void Block1( int a1, // Prevents access to global a1
char& b2 ) // Has same scope as c1 and d2
{
int c1; // A variable local to Block1
int d2; // Another variable local to Block1
.
.
.
}
//******************************************************************
void Block2()
{
int a1; // Prevents access to global a1
int b2; // Local to Block2; no conflict with b2 in Block1
while ()
{ // Block3
int c1; // Local to Block3; no conflict with c1 in Block1
int b2; // Prevents nonlocal access to b2 in Block2; no
// conflict with b2 in Block1
.
.
.
}
} |
|
|
|
|
|
|
|
|
Let's look at the ScopeRules program in terms of the blocks it defines and see just what these rules mean. Figure 81 shows the headings and declara- |
|
|
|
|
|