Membrii unei clase declarati folosiind cuvantul cheie static

Variabile statice

Pana acum variabilele membre ale unei clase apartineau numai unui obiect (mai precis unei instante a obiectului). Cuvantul cheie static permite ca o variabila sa apartina mai curand clasei decat unui obiect anume. Spre exemplu, dace au fost create 5 obiecte de tip Cat, fiecare are propria varsta, greutate ..etc., date care memoreaza starea unei pisici anume. Modificarea varstei uneia dintre ele nu le afecteaza pe celelalte.

Uneori este necesara totusi existenta unei zone comune de date la care sa aiba acces toate obiectele de acelasi tip. Spre exemplu, poate fi utila cunoastetea numarului de obiecte de acelasi tip create, sau existente, la un moment dat. Variabile membre statice indeplinesc acest deziderat: ele sunt comune tuturor obiectelor existente la un moment dat. Variabile statice reprezinta un compromis intre datele globale accesibile din orice loc al unui program si datele membre unor obiecte, care deregula sunt disponibile numai fiecarui obiect in parte.

Se poate gandi o variabila statiac ca apartinand clasei si nu unei instante anume a obiectului respectiv. Listingul urmator (stat_var.cpp) ilustreaza folosirea variabilelor statice

//Variabile membre declarate static
#include <iostream.h>
class Cat
{
public:
Cat(int age):itsAge(age){HowManyCats++; }
virtual ~Cat() { HowManyCats--; }
virtual int GetAge() { return itsAge; }
virtual void SetAge(int age) { itsAge = age; }
static int HowManyCats;

private:
int itsAge;
};
int Cat::HowManyCats = 0;

int main()
{
const int MaxCats = 5; int i;
Cat *CatHouse[MaxCats];
for (i = 0; i<MaxCats; i++)
CatHouse[i] = new Cat(i);

for (i = 0; i<MaxCats; i++)
{
cout << "There are ";
cout << Cat::HowManyCats;
cout << " cats left!\n";
cout << "Deleting the one which is ";
cout << CatHouse[i]->GetAge();
cout << " years old\n";
delete CatHouse[i];
CatHouse[i] = 0;
}
return 0;
}

Acest cod genereaza la iesire urmatoarele

There are 5 cats left!
Deleting the one which is 0 years old
There are 4 cats left!
Deleting the one which is 1 years old
There are 3 cats left!
Deleting the one which is 2 years old
There are 2 cats left!
Deleting the one which is 3 years old
There are 1 cats left!
Deleting the one which is 4 years old

Functii declarate folosind cuvantul cheie static

Functiile membre statice sun asemenea variabileleor membre statice: ele apartin clasei mai curand decat obiectului. Ele pot fi apelate chiar daca nu exista nici o instanta a vreunui obiect de acel tip. Exemplu urmator ilustreaza acest fapt.

#include <iostream.h>

class Cat
{
public:
Cat(int age):itsAge(age){HowManyCats++; }
virtual ~Cat() { HowManyCats--; }
virtual int GetAge() { return itsAge; }
virtual void SetAge(int age) { itsAge = age; }
static int GetHowMany() { return HowManyCats; }
private:
int itsAge;
static int HowManyCats;
};

int Cat::HowManyCats = 0;

void TelepathicFunction();

int main()
{
const int MaxCats = 5;
Cat *CatHouse[MaxCats]; int i;
for (i = 0; i<MaxCats; i++)
{
CatHouse[i] = new Cat(i);
TelepathicFunction();
}

for ( i = 0; i<MaxCats; i++)
{
delete CatHouse[i];
TelepathicFunction();
}
return 0;
}

void TelepathicFunction()
{
cout << "There are " << Cat::GetHowMany() << " cats alive!\n";
}

Acest cod genereaza urmatorul rezultat

There are 1 cats alive!
There are 2 cats alive!
There are 3 cats alive!
There are 4 cats alive!
There are 5 cats alive!
There are 4 cats alive!
There are 3 cats alive!
There are 2 cats alive!
There are 1 cats alive!
There are 0 cats alive!

Cornel Mironel Niculae, 2003-2004

04-Mar-2004