Reference Variables

Chapter 6 - Working with Data

Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
  Copyright 1998 The McGraw-Hill Companies

Storage Classes
Visual C/C++ supports four storage-class specifiers. They are
auto
register
static
extern
The storage class precedes the variable’s declaration and instructs the compiler how the variable should be stored. Items declared with the auto or register specifier have local lifetimes; items declared with the static or extern specifier have global lifetimes.
The four storage-class specifiers affect the visibility of a variable or function, as well as its storage class. Visibility (sometimes defined as scope) refers to that portion of the source program in which the variable or function can be referenced by name. An item with a global lifetime exists throughout the execution of the source program.
The placement of a variable or a function declaration within a source file also affects storage class and visibility. Declarations outside all function definitions are said to appear at the external level, while declarations within function definitions appear at the internal level.
The exact meaning of each storage-class specifier depends on two factors: whether the declaration appears at the external or internal level and whether the item being declared is a variable or a function.
Variable Declarations at the External Level
Variable declarations at the external level may only use the static or extern storage class, not auto or register. They are either definitions of variables or references to variables defined elsewhere. An external variable declaration that also initializes the variable (implicitly or explicitly) is a defining declaration:
static int ivalue1;      // implicit 0 by default
static int ivalue1 = 10  // explicit

int ivalue2 = 20;        // explicit
Once a variable is defined at the external level, it is visible throughout the rest of the source file in which it appears. The variable is not visible prior to its definition in the same source file. Also, it is not visible in other source files of the program unless a referencing declaration makes it visible, as described shortly.
You can define a variable at the external level only once within a source file. If you give the static storage-class specifier, you can define another variable with the same name and the static storage-class specifier in a different source file. Since each static definition is visible only within its own source file, no conflict occurs.
The extern storage-class specifier declares a reference to a variable defined elsewhere. You can use an external declaration to make a definition in another source file visible or to make a variable visible above its definition in the same source file. The variable is visible throughout the remainder of the source file in which the declared reference occurs.
For an external reference to be valid, the variable it refers to must be defined once, and only once, at the external level. The definition can be in any of the source files that form the program. The following C++ program demonstrates the use of the extern keyword:
//
//     Source File A – incomplete file do not compile.
//
#include <iostream.h>

extern int ivalue;                 // makes ivalue visible
                                  // above its declaration

int main( )
{
 ivalue++;                        // uses the above extern
                                  // reference
 cout << ivalue << “\n”;          // prints 11
 function_a( );

 return(0);
}

int ivalue = 10;                   // actual definition of
                                  // ivalue

void function_a(void)
{
 ivalue++;                        // references ivalue
 cout << ivalue << “\n”;          // prints 12
 function_b( );
}

————————————————————

//           
//     Source File B
//

#include <iostream.h>

extern int ivalue;                // references ivalue
                                 // declared in Source A

void function_b(void)
{
 ivalue++;
 cout <<(“%d\n”, ivalue);        // prints 13
}
Variable Declarations at the Internal Level
You can use any of the four storage-class specifiers for variable declarations at the internal level. (The default is auto.) The auto storage-class specifier declares a variable with a local lifetime. It is visible only in the block in which it is declared and can include initializers.
The register storage-class specifier tells the compiler to give the variable storage in a register, if possible. This specifier speeds access time and reduces code size. It has the same visibility as an auto variable. If no registers are available when the compiler encounters a register declaration, the variable is given the auto storage class and stored in memory.
ANSI C does not allow for taking the address of a register object. However, this restriction does not apply to C++. Applying the address operator (&) to a C++ register variable forces the compiler to store the object in memory, since the compiler must put the object in a location for which an address can be represented.
A variable declared at the internal level with the static storage-class specifier has a global lifetime but is visible only within the block in which it is declared. Unlike auto variables, static variables keep their values when the block is exited. You can initialize a static variable with a constant expression. It is initialized to zero by default.
A variable declared with the extern storage-class specifier is a reference to a variable with the same name defined at the external level in any of the source files of the program. The internal extern declaration is used to make the external-level variable definition visible within the block. The next program demonstrates these concepts:
int ivalue1=1; // incomplete file do not compile.

void main( )
{ // references the ivalue1 defined above
    extern int ivalue1;

 // default initialization of 0, ivalue2 only visible
 // in main( )
    static int ivalue2;

 // stored in a register (if available), initialized
 // to 0
    register int rvalue = 0;

 // default auto storage class, int_value3 initialized
 // to 0
    int int_value3 = 0;

 // values printed are 1, 0, 0, 0:
    cout << ivalue1 << rvalue \
         <<ivalue2 << int_value3;
    function_a( );
}

void function_a(void)
{
 // stores the address of the global variable ivalue1
    static int
*pivalue1= &ivalue1;

 // creates a new local variable ivalue1 making the
 // global ivalue1 unreachable
    int ivalue1 = 32;

 // new local variable ivalue2
 // only visible within function_a
    static int ivalue2 = 2;

    ivalue2 += 2;

 // the values printed are 32, 4, and 1:
    cout << ivalue1 << ivalue2 \
    <<
*pivalue1);
}
Since ivalue1 is redefined in function_a( ), access to the global ivalue1 is denied. However, by using the data pointer pivalue1 (discussed in Chapter 10), the address of the global ivalue1 was used to print the value stored there.
Variable Scope Review
To review, there are four rules for variable visibility, also called scope rules. The four scopes for a variable are the block, function, file, and program. A variable declared within a block or function is known only within the block or function. A variable declared external to a function is known within the file in which it appears, from the point of its appearance to the end of the file. A variable declared as external in one source file and declared as external in other files has program scope.
Function Declarations at the External Level
When declaring a function at the external or internal level, you can use either the static or the extern storage-class specifier. Functions, unlike variables, always have a global lifetime. The visibility rules for functions vary slightly from the rules for variables.
Functions declared to be static are visible only within the source file in which they are defined. Functions in the same source file can call the static function, but functions in other source files cannot. Also, you can declare another static function with the same name in a different source file without conflict.
Functions declared as external are visible throughout all source files that make up the program (unless you later redeclare such a function as static). Any function can call an external function. Function declarations that omit the storage-class specifier are external by default.

Books24x7.com, Inc 2000 –  


Visual C++ 6(c) The Complete Reference
Visual Studio 6: The Complete Reference
ISBN: B00007FYGA
EAN: N/A
Year: 1998
Pages: 207

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net