[ fromfile: statics.xml id: statics ]
The keyword static can be applied to local variables, class members, and global variables/functions. In each case, static means something different.
The keyword static can be applied to a local variable declaration to give the variable static storage class (Section 20.3).
A local static variable is created only once, and initialized the first time its declaration statement is processed by the running program.
It is destroyed when the program terminates.
A non-local static is created once, when the object module is loaded into memory, and is destroyed when the program terminates.
A static data member is a piece of data that is associated with the class itself rather than one that belongs to a particular object.
It does not affect the sizeof() an object of the class.
Each object of a class maintains its own set of non-static data members,
but there is only one instance of any static data member, and it can be shared by all objects of the class.
static members are preferable to (and can generally replace the use of) global variables because they do not pollute the global namespace.
static data members must be declared static in (and only in) the class definition.
Example 2.9. src/statics/static.h
[ . . . . ]
class Thing {
public:
Thing(int a, int b);
~Thing();
void display() const ;
static void showCount();
private:
int m_First, m_Second;
static int sm_Count;
};
[ . . . . ]
Figure 2.3 shows a UML class diagram for class Thing from Example 2.9.
Notice that the static members are underlined in the diagram.
A class member function that does not in any way access the non-static data members of the class can (and should) be declared static.
In Example 2.9, the static data member is a private counter that keeps track of the number of Thing objects that exist at any given moment.
The public static member function displays the current value of the static counter.
Each static data member must be initialized (defined) once outside the class definition, preferably in the corresponding class implementation file [15].
Example 2.10 shows how to initialize and use static members.
Example 2.10. src/statics/static.cpp
#include "static.h" #include <iostream> int Thing::sm_Count = 0;Thing::Thing(int a, int b) : m_First(a), m_Second(b) { ++sm_Count; } Thing::~Thing() { --sm_Count; } void Thing::display() const { using namespace std; cout << m_First << "$$" << m_Second; } void Thing::showCount() {
using namespace std; cout << "Count = " << sm_Count << endl; }
| Note | |
|---|---|
Notice that the term |
statics that are defined inside a function or a block of code are initialized when they are executed for the first time.
long nextNumber() {
int localvar(24);
static long nmber = 1000;
cout << nmber + localvar;
return ++nmber;
}
The first call to nextNumber() initializes localvar to 24 and nmber to 1000, displays 1024 on the screen, and returns 1001.
When the function returns, localvar is destroyed but nmber is not destroyed.
Each time that this function is called, localvar gets created and initialized to 24 again.
The static variable nmber persists between calls and holds onto the value that it obtained in the last call.
So, for example,
the next time the function is called, 1025 is displayed and 1002 is returned.
static initialization
A static object that is not defined in a block or function is initialized when its corresponding object module (Section 7.1) is loaded for the first time.
Most of the time, this is at program startup, before main() starts.
The order in which modules get loaded and variables get initialized is implementation dependent.
A static object is constructed once and persists until the program terminates.
A static data member is a static object that has class scope.
In Example 2.11, we make use of an internal block so that we can introduce some local objects that will be destroyed before the program ends.
Example 2.11. src/statics/static-test.cpp
Here is the compile and run.
src/statics> g++ -Wall static.cpp static-test.cpp src/statics> ./a.out Count = 0 Count = 2 Count = 4 Count = 2 src/statics>
[15] The exception to this rule is a static const int, which can be initialized in the class definition.
| Generated: $Date: 2009-09-08 12:15:32 -0400 (Tue, 08 Sep 2009) $ | © 2009 Alan Ezust and Paul Ezust. |