class
{
int A;
static int B;
public:
sample (int a){A=a,B+=a;}
static void func(sample s);
};
void sample::func(sample s)
{
cout<<"A="<<s.A<<",B="<<B<<endl;
}
int sample::B=0;
void main( )
{
sample s1(2),s(5);
sample :: func(s1);
sample :: func(s2);
}
为什么结果:
A=2,B=7
A=5,B=7
已经定义了static int B
int sample::B=0
B是静态数据已定义并赋值,为什么会有表达式B+=a出现未出错,而且s2(5)会影响到func(s1)的B值
{
int A;
static int B;
public:
sample (int a){A=a,B+=a;}
static void func(sample s);
};
void sample::func(sample s)
{
cout<<"A="<<s.A<<",B="<<B<<endl;
}
int sample::B=0;
void main( )
{
sample s1(2),s(5);
sample :: func(s1);
sample :: func(s2);
}
为什么结果:
A=2,B=7
A=5,B=7
已经定义了static int B
int sample::B=0
B是静态数据已定义并赋值,为什么会有表达式B+=a出现未出错,而且s2(5)会影响到func(s1)的B值