In C++, when modifying a data member in a class declaration, the static keyword specifies that one copy of the member is shared by all the instances of the class. When modifying a member function in a class declaration, the static keyword specifies that the function accesses only static members.
Example
// Example of the static keyword
static int i; // Variable accessible only from this file
static void func(); // Function accessible only from this file
int max_so_far( int curr )
{
static int biggest; // Variable whose value is retained
// between each function call
if( curr > biggest )
biggest = curr;
return biggest;
}
// C++ only
class SavingsAccount
{
public:
static void setInterest( float newValue ) // Member function
{ currentRate = newValue; } // that accesses
// only static
// members
private:
char name[30];
float total;
static float currentRate; // One copy of this member is
// shared among all instances
// of SavingsAccount
};
// Static data members must be initialized at file scope, even
// if private.
float SavingsAccount::currentRate = 0.00154;