The externalIntvariable defined on line 1 of first.cpp (Listing 18.4a) has
external linkage. Although it is defined in first.cpp,second.cppcan also
access it. The two js found in both files are const, which, by default, have internal link-
age. You can override the constdefault by providing an explicit declaration, as shown in
Listings 18.5a and 18.5b.
LISTING18.5A Overriding the constDefault
0: // file: first.cpp
1: extern const int j = 10 ;
LISTING18.5B Overriding the constDefault
0: // file: second.cpp
1: extern const int j ;
2: #include <iostream>
3: int main()
4: {
5: std::cout << “j is “ << j << std::endl ;
6: return 0 ;
7: }
Note that coutis called on line 5 with the namespace designation of std. When built,
this example displays the following:
j is 10
Static Global Variables ..................................................................................
The standards committee has deprecated the use of static global variables. Static global
variables are declared as follows:
static int staticInt = 10 ;
int main()
{
//...
}
The use of staticto limit the scope of external variables is no longer recommended and
might become illegal in the future. You should now use namespaces instead of static.
Of course, to do this, you need to know how to create one.
642 Day 18
ANALYSIS