LISTING18.2B Second Listing for Identifier Hiding
0: // file second.cpp
1: int integerValue = 0 ;
2: // end of second.cpp
640 Day 18
Note the use of the scope resolution operator ::, indicating that the
integerValuebeing referred to is global, not local.
NOTE
The problem with the two global integers defined outside of any functions is that
they have the same name and visibility, and, thus, cause a linker error.
Visibility of Variables ....................................................................................
The term visibilityis used to designate the scope of a defined object, whether it is a vari-
able, a class, or a function. Although this was covered on Day 5, “Organizing into
Functions,” it is worth covering again here briefly.
As an example, a variable declared and defined outside any function hasfile, or global,
scope. The visibility of this variable is from the point of its definition through the end of
the file. A variable having a block, or local, scope is found within a block structure. The
most common examples are variables defined within functions. Listing 18.3 shows the
scope of variables.
LISTING18.3 Working Variable Scope
0: // Listing 18.3
1: int globalScopeInt = 5 ;
2: void f( )
3: {
4: int localScopeInt = 10 ;
5: }
6: int main( )
7: {
8: int localScopeInt = 15 ;
9: {
10: int anotherLocal = 20 ;
11: int localScopeInt = 30 ;
12: }
13: return 0 ;
14: }
ANALYSIS