2.8 Scope


The scope of a variable or object refers to that portion of the program where the variable or object is visible (i.e., where it can be manipulated). The objects used in the previous sections have a scope that is identical to the scope of the variable that names the object. The effect of scope on objects is discussed in this section. A different way of creating objects that are affected differently by scope is discussed in the next section.

Scope may be global or local. An object with global scope is declared as a global variable, and can be accessed from anywhere in the program. Global objects are constructed immediately before the program begins execution and are destructed after the program has terminated normally. Objects with local scope are constructed at the site of their declaration and are destructed when control reaches the end of the program unit (function, method, block) that contains the declaration.

Global and local scopes are illustrated in the following example in which the globalWindow object has global scope; it can be accessed from within the function, from within the for loop, and from within the then clause. The function Window object has a local scope that is the body of the function. Similarly the loopWindow object has a scope limited to the for loop and the ifWindow object has a local scope limited to the then clause of the if construct.

  Frame globalWindow;                  // global scope

   void function() {
        Frame functionWindow;           // start of functionWindow scope
        ...
        for( int i=0; i<10; i++) {
           Frame loopWindow;            // start of loopWindow scope
           ...
                if (i < 5) {
                   Frame ifWindow;      // start of ifWindow scope
                   ...
                }                       // end of ifWindow scope

        }                               // end of loopWindow scope

   }                                    // end of functionWindow scope

While objects with global scope exist throughout the execution of the entire program, objects with local scope do not. For example, the functionWindow object is constructed when control enters the function and is destructed when control leaves the function via a return or by encountering the end of the function. Similarly, the loopWindow object is constructed and destructed on each iteration of the loop and the ifWindow is constructed when the then clause is entered and destructed when the end of the then clause is reached.

 

Tasks

  1. Assume that a program calls function() in the above example two times. How many objects are constructed/destructed throughout the entire execution of the program?
  2. Name three other program units that create a distinct scope. For each of the three program units, write a short example similar to the example above showing an object local to that scope.
  3. What is a major problem with using global variables? Does this problem also apply to global objects? Explain why or why not.
  4. Write a program similar to the function in the example in this section. Execute the program and objserve the creation and destruction of the windows as the scopes are entered and exited. Add some detail to the original code so that the windows from different scopes are far apart on the screen while those in the same scope are near, but not on top of, each other.



©1998 Prentice-Hall, Inc.
A Simon & Schuster Company
Upper Saddle River, New Jersey 07458

Legal Statement

 

 

 

ÿ