//一般而言,静态成员函数不访问类中的非静态成员。若确实需要,静态成员函数只能通过对象名(或指向对象的指针)访问该对象的非静态成员。
#include<iostream>
using namespace std;class Ctype
{
private :
int a;
static int s;
public :
static void Print();
Ctype();
};
void Ctype::Print()
{
//cout<<"a="<<a++<<endl;//错误:动态变量不能被静态函数访问
cout<<"s="<<s<<endl;
}
Ctype::Ctype()//构造函数能访问静态
{
a=0;
s++;
cout<<"a="<<+a<<endl;
}
int Ctype::s=0;// //静态数据成员赋初值在类体外进行,前面不能再加static
int main()
{
Ctype::Print();//在未定义对象时,可以用类名来调用静态成员函数
Ctype c1,c2;
c1.Print();
c2.Print();
Ctype c3;
c3.Print();
return 0;
}