Static Members


Data members and methods of a class may be declared to be static. A single copy of a static member is shared amongst all instances of a class. Unlike non-static members, a static member is required to be defined separately.

Consider the following example.

class test
{
 public:
  static int i;                    // static member - declared but not defined
  int j;                           // non-static member
 
  static int value_i() {return i;} // static methods have access only to static members
  int value_j() {return j;}        // non-static methods can access both static and non-static members
};
 
int test::i = 0;                   // member requires definition outside the class
 
test test_a, test_b;               // test_a and test_b share member i but have separate j's.

Notes