在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;
}