|
|
|
|
|
|
|
through all the surrounding rooms to the declarations of the global variables (and anything between). You would not be able to see into any other rooms (such as Block1), however, because their mirrored outer surfaces would block your view. Because of this analogy, the term visible is often used in describing a scope of access. For example, variable a2 is visible throughout the program, meaning that it can be accessed from anywhere in the program. |
|
|
|
|
|
|
|
|
Figure 81 does not tell the whole story; it represents only scope rules 1 through 4. We also must keep rule 5 in mind. Variable a1 is declared in two different places in the ScopeRules program. Because of name precedence, Block2 and Block3 access the a1 declared in Block2 rather than the global a1. Similarly, the scope of the variable b2 declared in Block2 does not include the hole created by Block3, because Block3 declares its own variable b2. |
|
|
|
|
|
|
|
|
Name precedence is implemented by the compiler as follows. When a statement refers to an identifier, the compiler first checks the local declarations. If the identifier isn't local, the compiler works its way outward through each level of nesting until it finds an identifier with the same name. There it stops. If there is an identifier with the same name declared at a level even further out, it is never reached. If the compiler reaches the global declarations (including identifiers inserted by #include directives) and still can't find the identifier, an error message such as UNDECLARED IDENTIFIER will result. |
|
|
|
|
|
|
|
|
Such a message most likely indicates a misspelling or an incorrect capitalization, or it could mean that the identifier was not declared before the reference to it or was not declared at all. It may also indicate, however, that the blocks are nested so that the identifier's scope doesn't include the reference. |
|
|
|
|
|
|
|
|
Variable Declarations and Definitions |
|
|
|
|
|
|
|
|
In Chapter 7, you learned that C++ terminology distinguishes between a function declaration and a function definition. A function prototype is a declaration onlythat is, it doesn't cause memory space to be reserved for the function. In contrast, a function declaration that includes the body is called a function definition. The compiler reserves memory for the instructions in the function body. |
|
|
|
|
|
|
|
|
C++ applies the same terminology to variable declarations. A variable declaration becomes a variable definition if it also reserves memory for the variable. All of the variable declarations we have used from the beginning have been variable definitions. What would a variable declaration look like if it were not also a definition? |
|
|
|
|
|
|
|
|
In the previous chapter, we talked about the concept of a multifile program, a program that physically occupies several files containing individual pieces of the program. C++ has a reserved word extern that lets you reference a global variable located in another file. A normal declaration such as |
|
|
|
|
|