Ensuring You Have Only One Instance of a Variable Across Multiple Source Files

Problem

You need the same variable to be used by different modules in a program, and you can only have one copy of this variable. In other words, a global variable.

Solution

Declare and define the variable in a single implementation file in the usual manner, and use the extern keyword in other implementation files where you require access to that variable at runtime. Often, this means including the extern declarations in a header file that is used by all implementation files that need access to the global variable. Example 2-3 contains a few files that show how the extern keyword can be used to access variables defined in another implementation file.

Example 2-3. Using the extern keyword

// global.h
#ifndef GLOBAL_H_ _ // See Recipe 2.0
#define GLOBAL_H_ _

#include 

extern int x;
extern std::string s;

#endif

// global.cpp
#include 

int x = 7;
std::string s = "Kangaroo";

// main.cpp
#include 
#include "global.h"

using namespace std; 

int main( ) {

 cout << "x = " << x << endl;
 cout << "s = " << s << endl;
}

 

Discussion

The extern keyword is a way of telling the compiler that the actual storage for a variable is allocated somewhere else. extern tells the linker that the variable it qualifies is somewhere in another object file, and that the linker needs to go find it when creating the final executable or library. If the linker never finds the extern variable you have declared, or it finds more than one of definition for it, you will get a link error.

Example 2-3 isn't terribly exciting, but it illustrates the point well. My two global variables are declared and defined in global.cpp:

int x = 7;
std::string s = "Kangaroo";

I need to be able to access them from other implementation files, so I put an extern declaration for them in the header file global.h:

extern int x;
extern std::string s;

The distinction between declaration and definition is important. In C++, you can declare something many times, so long as the declarations match, but you may only define something once; this is called the one-definition rule (you can actually define it more than once, in some cases, but only if the definitions are exactly the sameusually this is not a good idea). The extern keyword is a mechanism for telling the compiler and linker that the definition is somewhere else, to be resolved at link time.

This is not to say that using extern should be a regular practice. You should use it sparingly, and only when you have to, since it permits application-global variables. Sometimes you may need this for truly global objects or dataa logging object; a piece of hardware; a large, shared data objectbut most of the time there are more appropriate design alternatives.

Building C++ Applications

Code Organization

Numbers

Strings and Text

Dates and Times

Managing Data with Containers

Algorithms

Classes

Exceptions and Safety

Streams and Files

Science and Mathematics

Multithreading

Internationalization

XML

Miscellaneous

Index



C++ Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2006
Pages: 241

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