代码:
#include <iostream>
using namespace std;
class Test
{
private:
static int val;
int a;
public:
static int func();
static void sfunc(Test &r);
};
int Test::val=20;
int Test::func()
{
val+=val;
return val;
}
void Test::sfunc (Test &r)
{
r.a=25;
cout<<"Result3="<<r.a<<endl;
}
int main()
{
cout <<"Resultl="<<Test::func()<<endl;
Test a;
cout<<"Result2="<<a.func()<<endl;
Test::sfunc (a);
return 0;
}
运行结果:
学习心得:
定义val为常对象,赋值为20,定义func和sfunc为常成员函数。