类里面static修饰的成员,成为静态类成员。
类的静态成员是该类型的所有对象对象所共享。
静态成员的定义及使用
class Date
{
public :
Date ()
{
cout<<"Date ()" <<endl;
++ sCount;
}
void Display ()
{
cout<<"year:" <<_year<< endl;
cout<<"month:" <<_month<< endl;
cout<<"day:" <<_day<< endl;
}
// 静态成员函数
static void PrintCount()
{
cout<<"Date count:" <<sCount<< endl;
}
private :
int _year ; // 年
int _month ; // 月
int _day ; // 日
private :
static int sCount; // 静态成员变量,统计创建时间个数
};
// 定义并初始化静态成员变量
int Date::sCount = 0;
void Test ()
{
Date d1 ,d2;
// 访问静态成员
Date::PrintCount ();
}
原文:http://10622551.blog.51cto.com/10612551/1697121