23.3 The extern Storage Class

I l @ ve RuBoard

The extern storage class is used to indicate that a variable or function is defined outside the current file but is used in this file. Example 23-1 illustrates a simple use of the extern modifier.

Example 23-1. tf/main.cpp
 #include <iostream> /* number of times through the loop */ extern int counter; /* routine to increment the counter */ extern void inc_counter(  ); int main(  ) {     int   index; /* loop index */     for (index = 0; index < 10; ++index)         inc_counter(  );     std::cout << "Counter is " << counter << '\n';     return (0); } 
Example 23-2. File: tf/count.cpp
 /* number of times through the loop */ int counter = 0; /* trivial example */ void inc_counter(  ) {     ++counter; } 

The function main uses the variable counter . Because counter is not defined in main , it is defined in the file count.cpp . The extern declaration is used by main.cpp to indicate that counter is declared somewhere else, in this case the file count.cpp . The modifier extern is not used in this file, because this is the "real" declaration of the variable.

Actually, three storage class identifiers can be used to indicate the files in which a variable is defined, as shown in Table 23-1.

Table 23-1. Storage class identification

Modifier

Meaning

extern

Variable/function is defined in another file. (The variable can also be defined in the current file.)

<blank>

Variable/function is defined in this file (public) and can be used in other files.

static

Variable/function is local to this file (private).

Notice that the keyword static has two meanings. (It is the most overworked keyword in the C++ language. For a complete list of the meanings of static see Table 14-1.) For data defined globally, static means "private to this file." For data defined inside a function, it means "variable is allocated from static memory (instead of the temporary stack)."

C++ is very liberal in its use of the rules for static , extern, and <blank> storage classes. It is possible to declare a variable as extern at the beginning of a program and later define it as <blank>.

 extern sam;  int sam = 1;    // This is legal 

This ability is useful when you have all your external variables defined in a header file. The program includes the header file (and defines the variables as extern ), and then defines the variable for real.

Another problem concerns declaring a variable in two different files.

  File:  main.cpp  int     flag  = 0;      // Flag is off int main(  )  {      std::cout << "Flag is " << flag << '\n';  }  File: sub.cpp  int     flag = 1;       // Flag is on 

What happens in this case? There are several possibilities:

  • flag could be initialized to 0 because main.cpp is loaded first.

  • flag could be initialized to 1 because the entry in sub.cpp overwrites the one in main.cpp .

  • I don't know, but whatever it is, it's probably bad.

In this case, there is only one global variable called flag . It will be initialized to either 1 or 0 depending on the whims of the compiler. (The good ones generate an error.) It is entirely possible for the program main to print out:

 flag is 1 

even though we initialized it to 0. To avoid the problem of hidden initializations, use the keyword static to limit the scope of variables to the file in which they are declared.

Say we wrote the following:

  File: main.cpp  static int      flag  = 0;      // Flag is off int main(  )  {          std::cout << "Flag is " << flag << '\n';  }  File: sub.cpp  static int      flag = 1;       // Flag is on 

In this case, flag in main.cpp is an entirely different variable from flag in sub.cpp . However, you should still give the variables different names to avoid confusion.

I l @ ve RuBoard


Practical C++ Programming
Practical C Programming, 3rd Edition
ISBN: 1565923065
EAN: 2147483647
Year: 2003
Pages: 364

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