Wrox Press C++ Tutorial
Both data members and function members of a class can be declared as static. Because the context is a class definition, there's a little more to it than the effect of the keyword static outside of a class, so let's look at static data members.
When we declare data members of a class as static, they are defined only once and are shared between all objects of the class. Each object gets its own copies of each of the ordinary data members of a class, but only one instance of each static data member exists, regardless of how many class objects have been defined.

One use for a static data member is to count how many objects actually exist. We could add a static data member to the public section of the Box class by adding the following statement to the previous class definition:
static int ObjectCount; // Count of objects in existence
We now have a problem. How do we initialize the static data member? We can't put it in the class definition - that's simply a blueprint for an object, and initializing values are not allowed. We don't want to initialize it in a constructor, because we want to increment it every time the constructor is called. We can't initialize it in another member function, since a member function is associated with an object and we want it initialized before any object is created. The answer is to write the initialization outside of the class definition with this statement:
// Initialize static member of class Box
int Box::ObjectCount = 0;
Notice that the keyword static is not included here. However, we do need to qualify the member name by using the class name and the scope resolution operator so that the compiler understands that we're referring to a static member of the class. Otherwise, we would simply create a global variable that was nothing to do with the class.
Let's add the static data member and the object counting capability to the last example.
// Ex6_11.cpp
// Using a static data member in a class
#include <iostream>
using namespace std;
class Box // Class definition at global scope
{
public:
static int ObjectCount; // Count of objects in existence
// Constructor definition
Box(double lv, double bv=1.0, double hv=1.0)
{
cout << endl << "Constructor called.";
length = lv; // Set values of
breadth = bv; // data members
height = hv;
ObjectCount++;
}
Box() // Default constructor
{
cout << endl
<< "Default constructor called.";
length = breadth = height = 1.0;
ObjectCount++;
}
// Function to calculate the volume of a box
double Volume()
{
return length * breadth * height;
}
private:
double length; // Length of a box in inches
double breadth; // Breadth of a box in inches
double height; // Height of a box in inches
};
// Initialize static member of class Box
int Box::ObjectCount = 0;
int main(void)
{
Box Boxes[5]; // Array of Box objects declared
Box Cigar(8.0, 5.0,1.0); // Declare Cigar box
cout << endl << endl
<< "Number of objects (through class) = "
<< Box::ObjectCount;
cout << endl
<< "Number of objects (through object) = "
<< Boxes[2].ObjectCount;
cout << endl;
return 0;
}
This example will produce the output:

This code shows that it doesn't matter how we refer to the static member ObjectCount (whether through the class itself or any of the objects of that class). The value is the same and it is equal to the number of objects of that class that have been created. The six objects are obviously the five elements of the Boxes array, plus the object Cigar. It's interesting to note that static members of a class exist even though there may be no members of the class in existence. This is evidently the case, since we initialized the static member ObjectCount before any class objects were declared.
You must initialize a static data member, or the compiler will complain. The declaration in the class definition doesn't define a static variable so, until you define its initial value, it doesn't exist.
By declaring a function member as static, you make it independent of any particular object of the class. Referencing members of the class from within a static function must be done using qualified names (as you would do with an ordinary global function accessing a public data member). The static member function has the advantage that it exists and can be called even if no objects of the class exist. In this case, only static data members can be used, since they are the only ones that exist. Thus, you can call a static function member of a class to examine static data members, even when you do not know for certain that any objects of the class exist. You could, therefore, use a static member function to determine whether some objects of the class have been created or, indeed, how many have been created.
Of course, once the objects have been defined, a static member function can access private as well as public members of class objects. A static function might have this prototype:
static void afunction(int n);
A static function can be called in relation to a particular object by a statement such as the following,
aBox.afunction(10);
where aBox is an object of the class. The same function could also be called without reference to an object. In this case, the statement would take the following form,
Box::afunction(10);
where Box is the class name. Using the class name and the scope resolution operator serves to tell the compiler to which class the function afunction() belongs.