可能重複:
file scope and static floats
What are static variables?靜態對象初始化C++
這裏是從一本書的碼。
class X
{
int i;
public:
X(int ii = 0) : i(ii) {cout<<i<<endl;} // Default
~X() { cout << "X::~X()" << endl; }
};
void f()
{
static X x1(47);
static X x2; // Default constructor required
}
int main()
{
f();
return 0;
}
我的問題是爲什麼我會喜歡在函數f()中聲明一個靜態對象?如果我沒有將x1和x2聲明爲靜態會發生什麼?
感謝您的解釋。看起來,即使我在全局空間中有一個新函數並調用f(),當使用靜態並且不使用靜態f()時,它也不起作用。 – macroland