static 成员在类内声明;
在类外初始化,不受访问权限的限制, 使用类名访问获取时受限制
类内加 inline声明可直接进行初始化
#include <iostream>
#include <conio.h>
using namespace std;
class test
{
public:
static int i;
test() {}
void print();
void setInt(){
i = 100;
j = 200;
}
private:
inline static int j = 50;
};
int test::i = 0;
//int test::j = 10;
void test::print()
{
cout << "i = "<< i << endl;//对比参照项1
cout << "j = "<< j << endl;//对比参照项2
}
int main()
{
test t;
t.print();
cout << "i = "<< t.i << endl;//对比参照项3
//cout << "j = "<< t.j << endl;//测试是否可以不受private作用
t.i = 10;
cout << "i = " << test::i<< endl;
test s;
s.print();
s.setInt();
cout << "---------------------------------"<< endl;
t.print();
cout << "---------------------------------"<< endl;
s.print();
getch();
return 0;
}