静态成员变量是同一个类的所有实例共享的变量,他们是全局数据(可供所有部分使用)和成员数据(通常只供一个对象使用)的折衷。

#include <iostream>

class Cat
{
public:
Cat(int newAge = 1):age(newAge){ howManyCats++; }
virtual ~Cat() { howManyCats--; }
virtual int getAge() { return age; }
virtual void setAge(int newAge) { age = newAge; }
static int howManyCats;

private:
int age;
};

int Cat::howManyCats = 0;

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

for (i = 0; i < maxCats; i++)
{
std::cout << "There are ";
std::cout << Cat::howManyCats;
std::cout << " cats left!\n";
std::cout << "Deleting the one which is ";
std::cout << catHouse[i]->getAge();
std::cout << " years old\n";
delete catHouse[i];
catHouse[i] = 0;
}
return 0;
}

howmanycats变量是公有的,可在main中直接访问。只要总是通过cat实例来访问数据,就应将该成员变量及其他成员函数声明为私有的,并提供公有的存取器函数。另一方面,如果想在没有cat对象的情况下直接访问数据,有两种选择:将他声明为公有变量或提供一个静态成员函数。

通过静态成员函数访问私有静态变量

#include <iostream>

class Cat
{
public:
Cat(int newAge = 1):age(newAge){ howManyCats++; }
virtual ~Cat() { howManyCats--; }
virtual int gGetAge() { return age; }
virtual void setAge(int newAge) { age = newAge; }
static int getHowMany() { return howManyCats; }
private:
int age;
static int howManyCats;
};

int Cat::howManyCats = 0;

void countCats();

int main()
{
const int maxCats = 5;
Cat *catHouse[maxCats];
int i;
for (i = 0; i < maxCats; i++)
{
catHouse[i] = new Cat(i);
countCats();
}

for (i = 0; i < maxCats; i++)
{
delete catHouse[i];
countCats();
}
return 0;
}

void countCats()
{
std::cout << "There are " << Cat::getHowMany()
<< " cats alive!\n";
}

由于getHowMany() 是公有的,任何函数都可访问它;同时,他还是静态的,因此不通过cat对象也可以调用它。因此在第41行,函数countCats()能够访问该公有的静态存取器函数,虽然他没有cat对象可用。当然,也可以在main()函数中,通过可用的cat对象来调用getHowMany() 函数,就像调用其他存取器函数一样。

注意:静态成员函数没有this指针。因此不能将他们声明为const。另外,由于在成员函数中是通过this指针来访问成员数据变量的,因此静态成员函数不能访问非静态成员变量。