在C++中,静态变量可以在类的声明或定义时进行初始化赋值。具体有以下几种方法:
class MyClass {
public:
static int myStaticVar;
};
int MyClass::myStaticVar = 100;
class MyClass {
public:
static int myStaticVar;
};
int MyClass::myStaticVar = 100;
class MyClass {
public:
static const int myStaticVar = 100;
};
class MyClass {
public:
static int myStaticVar;
};
int MyClass::myStaticVar = 100;
int main() {
MyClass::myStaticVar = 200; // 修改静态变量的值
return 0;
}