class F{
int a;
int b;
public:
static int max();
F plus(const F& rh)const{ //为了保持对象不变,用const修饰
F res=(a+rh.a,b+rh.b);
return res;
}
//上面res只用了一次,可以不使用,用匿名对象
F plus(const F& rh)const{
return F (a+rh.a,b+rh.b);//匿名对象
}
};
max()为静态成员函数,它不属于某一个类。不依赖于当前对象,操作的是整个类的数据,它的操作是影响整个类的。
F a;
a.max(); //这样调用也可以,编译器会将其编译成Typeof(a)::max();
F::max(); //正确调用方法;
静态成员变量必须在类外面初始化。它是属于整个类的。
/****************************************************************************/
F A.plus(B);
/*******************************************************************************/