我正在为我的期末考试而学习,最近有一个话题困扰着我,我不明白为什么我们在代码中使用静态变量或静态数据成员。谁能向我解释一下我们如何以及为什么static在 C++ 中使用关键字
我尝试在不同的网站上查找它,并且尝试了一些代码,但我不明白为什么我会得到这样的结果。
class myclass {
public:
int a,b;
inline int getVal();
};
inline int myclass :: getVal()
{
cout<<"Enter the value of a and b\n";
static int a = 9; //static keyword used.
cin>>a>>b;
}
int main()
{
myclass o1;
o1.getVal();
cout<<"\nThe value of a is : "<<o1.a<<"\nThe value of b is : "<<o1.b;
}
无论我输入什么,我得到的 a 值都是 3?谁能向我解释这是为什么?