1.类中的静态成员函数访问此类中的非静态成员时只能通过对象名,访问静态成员时既能通过对象名也能通过类名。
2非静态的成员访问静态成员时只能通过对象。
3静态成员变量的初始化只能放在所有函数的外面。
对此我们可以举个例子:
class A
{
public :
A(int a=0){x=a;}
static void f1();
static void f2(A a);
private :
int x;
static int y;
};
void A::f2(A a)
{
cout << A::y;
//cout << x; Error
cout << a.x; //OK
}
void A::f1()
{
cout << A::y<<endl;
}
int main()
{
A::f1();//通过类名直接访问静态成员函数
A mA(3);
A ::f2(mA);
mA.A::f1();
return 0;
}