### Static Members

Class members can be declared using the storage-class specifier static in the class member list. Only one copy of the static member is shared by all objects of a class in a program. When you declare an object of a class having a static member, the static member is not part of the class object.

A typical use of static members is for recording data common to all objects of a class. For example, you can use a static data member as a counter to store the number of objects of a particular class type that are created. Each time a new object is created , this static data member can be incremented to keep track of the total number of objects.

The declaration of a static member in the member list of a class is not a definition. The definition of a static member is equivalent to an external variable definition. You must define the static member outside of the class declaration.

For example:

    class X
    {
    public:
          static int i;
    }
    int X::i = 0; // definition outside class declaration
    //      .
    //      .
    //      .

A static member can be accessed from outside of its class only if it is declared with the keyword public. You can then access the static member by qualifying the class name using the :: (scope resolution) operator. In the following example:

    class X
    {
    public:
          static int f();
    };
    void main ()
    {
          X::f();
    }

you can refer to the static member f() of class type X as X::f().

Using the Class Access Operators with Static Members

Class Member Lists
Static Data Members
Static Member Functions
Public
static