< Previous | Contents | Next >
Declaring and Defining Static Member
A static member function exists for the entire class. I declare a static member function in Critter with the following line:
static int GetTotal(); //static member function prototype
You can declare your own static member function like I did, by starting the declaration with the keyword static. Static member functions are often written to work with static data members.
I define the static member function GetTotal() that returns the value of the static data member s_Total.
int Critter::GetTotal() //static member function definition
{
return s_Total;
}
A static member function definition is much like the non-static member function definitions you’ve seen so far. The major difference is that a static member function cannot access non-static data members. This is because a static member function exists for the entire class and is not associated with any particular instance of the class.