從21世紀城市本書靜態變量:在程序啓動時,前主,所以你不能初始化它們 初始化有非常數值
靜態變量,即使是那些在函數中,初始化 具有非恆定價值。
//this fails: can't call gsl_vector_alloc() before main() starts static gsl_vector *scratch = gsl_vector_alloc(20);
這是一種煩惱,但與宏迎刃而解開始在零 和分配上首次使用:
#define Staticdef(type, var, initialization) \ static type var = 0; \ if (!(var)) var = (initialization); //usage: Staticdef(gsl_vector*, scratch, gsl_vector_alloc(20));
我不明白,對宏進行什麼區別。預處理後它不完全一樣嗎?
不通過這本書非常安全的建議,但更多的是快速和骯髒的劈不安全。 – alk 2014-11-03 15:57:32