Graham, Neil. Learning C++. McGraw Hill, Inc. 1991. pp. 309-310
The member variables declared in a class declaration are normally instance variables – a separate copy of them is created for each class object. Sometimes, however, it is convenient to have class variables that are associated with the class itself rather than with any class object. Every instance of a class accesses the same class variables, although each has its own private copy of any instance variables.
In C++, class variables are declared as static. Thus in
class c {
int
i;
int
j;
static int m;
static int n;
public:
void zap();
static void
clear();
};
void c::zap() {
i = 0; j = 0; m =
0; n = 0;
}
void c::clear() {
m = 0; n = 0;
}
i and j are instance variables and m and n are class variables. Every object of class c will have its own private i and j, which can have different values for different objects; however, all objects will access the same m and n, which will, of course, have the same values for all objects.
Static variables are like non-inline member functions in that they are declared in a class declaration and defined in the corresponding source file. To define static variables m and n, the source file for class c must contain the following declarations (which are also definitions):
int
c::m;
int
c::n;
These definitions can also be used to assign initial values to the static member variables. For example, the following definitions give m the initial value 5 and n the initial value 6:
int
c::m = 5;
int
c::n = 6;
The accessibility rules for class variables are the same as for instance variables. Thus private and protected class variables can be accessed by member and friend functions, but not by functions that are not associated with the class. Likewise, a derived class with a public base class inherits access to the protected and public class variables of the base class.
In addition, we can define static member functions that can manipulate only static member variables – they cannot access the instance variables of class objects. Like static member variables, static member functions are associated with a class rather than with any class object. A static member function is invoked via the name of the class rather than via the name of a class object.
The preceding example defines an ordinary member function zap() and a static member function clear(). To invoke the ordinary member function zap(), we must declare a class object cc and apply zap() to it with the dot operator:
c cc; // declare cc as c object
cc.zap(); // apply zap() to
class object cc
From the definition of zap(), we note that it can manipulate the instance variables i and j of cc as well as the class variables m and n.
In contrast, the static member function clear() is invoked using the name of the class and the :: operator:
c::clear();
We can call clear() even if no class objects have been
created. From the definition of clear(), we note that it manipulates only
the class variables m and n; clear()
does not have access to the instance variables i and j of any class object.