Assigned Reading: 3.1-3.5
Handouts (available on-line):
Topics Covered:
Our next example shows that a static local variable can be used to count the number of times that a function is called. Note in the sample run that the address of a static local variable is very different from automatic local variables.
When the keyword "static" is placed in front of a global variable, this tells the linker not to use the global variable outside the file. So, static here, in a sense, changes the scope of that variable. It certainly does not affect the allocation method of the global variable. All global variables, static or otherwise, are stored in permanent locations. Our program and sample run of a file with a static global variable shows that the compiler complains about not finding a variable called info.
To make things even more confusing, C++ also allows static member functions. Here the use of the keyword static is very hard to justify. The main difference between a normal member function and a static member function is that a static member function can be invoked without using an object of that class. In our Widget class example, there is a static member function called report(). This function can be invoked using the syntax Widget::report() from the main function. Again, the use of "static" here changes the scope of the identifier report rather than its allocation method. Did Dante have a special circle in Hell for people who invent especially confusing terminology?