/*本程序选自thinking in c++ P430
* 本程序的目的是为了说明类的静态成员函数的使用情况。
*记住:Because the static member functions have no this pointer,so they
* cannot neither access non-static data members nor non-static member functions.
* they can access only static data members and call only other static member functions.
*/
class X {
int m_i;
static int j;
public:
X( int ii=0 ) : m_i(ii) {
//Non-static member function can access static member function
//or data:
j = m_i;
}
int val() const { return m_i;}
static int incr() {
//! ++ m_i; Error : static member function cannot access non-static
// member data;
return ++j; //ok! access static member data;
}
static int f(){
//! val(); //error :static member function cannot access non-static
//function
return incr(); //OK! call static member function;
}
};
int X::j = 0;
int main()
{
X x;
X *xp = &x;
// 静态成员函数的调用方法
x.f();
xp->f();
X::f();
}