关于c++的静态成员

来源:百度知道 编辑:UC知道 时间:2024/05/15 17:30:49
class A{
private:
static int c;
public:
int getC(){
return c;
}
}
int A::c=1;

A a;
cout<<a.getC()<<endl;
如果我调用getC();会得到结果1;
但是如果我把getC()函数改为return this->c就会得到一个随机数。
为什么?

#include <iostream.h>

class A
{
private:
static int c;
public:
int getC()
{
return this->c;
}
};

int A::c=1;

void main()
{
A a;
cout << a.getC() << endl;
}

得到的结果还是1

可能是你其它地方弄错了吧,我试了一下,是1

可能是你其它地方弄错了吧,我试了一下,是1