1: //storage_linkage_main3.cpp
2: //Goes with storage_linkage_aux3.cpp. Compile the two files
3: //separately and link the object files to produce an executable.
4: //Illustrates the use of the keyword "static" applied to a function
5: //defined in the global namespace to restrict the visibility of
6: //that function to the file in which it is defined.
8: #include <iostream>
9: using namespace std;
11: void SayHi();
12: void DisplayGreeting();
14: int main()
15: {
16: SayHi();
17: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
19: DisplayGreeting();
20: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
21: }
23: void SayHi()
24: {
25: cout << "\nHi from the main file!";
26: }
28: /*
29: Questions:
30: 1. What is the effect of commenting out the above definition of SayHi()?
31: 2. What is the effect of commenting out the above definition of SayHi()
32: and simultaneously commenting out the keyword static in the companion
33: file storage_linkage_aux3.cpp?
35: Answers:
36: 1. There will be a lilnk-time error since a SayHi() function definition
37: can no longer be found. (The visibility of the one in the companion
38: file is limited to that file.)
39: 2. In this case, the SayHi() function defined in the companion file is
40: used twice. It is the one called directly in main in this file, and
41: is also the one called indirectly (by DisplayGreeting() from the
42: companion file). Thus the program compiles and links fine, and we get,
43: twice, "Hi from the auxiliiary file!"
44: */