看板 C_and_CPP 關於我們 聯絡資訊
問題(Question): 我是無意間看到 http://www.tenouk.com/ModuleZ.html 裏面提到: Static storage class can be specified for automatic as well as external variables such as: static extern varx; 可是我無法理解static怎麼會跟extern合用。 extern是表示該變數被定義在所在的frame以外要另外去找 static是表示該變數被侷限在所在的frame以內 合用意味著?所以在哪些情況下需要合用? static auto 似乎比較好理解,兩者都是把變數侷限(這樣好像多此一舉?) 不知道有無這種需要兩種以上keyword定義variable的情況? -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.32.244.234 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1448803181.A.932.html
OPIV: 沒有 static extern 這種東西… 11/29 21:42
OPIV: 但是你可以 static int foo; void bar(void) { extern int f 11/29 21:44
OPIV: oo; } 11/29 21:44
OPIV: 在 scope 內繼續使用 scope 外的變數 11/29 21:45
OPIV: 同樣的,也沒有 static auto variable 11/29 21:48
OPIV: 如果你這樣 static int foo; void bar(void) { auto int foo 11/29 21:48
OPIV: ; } 11/29 21:49
OPIV: 那麼代表在 scope 內覆蓋掉 global 的宣告,宣告一個新的 au 11/29 21:50
OPIV: to 變數 11/29 21:50
Caesar08: static auto只在C++11之後才生效,C++11之前是不合法的 11/29 22:34
OPIV: 不過c++11的auto已經不只是儲存等級就是了 11/29 22:57
wtchen: c++11 之前 auto給我的感覺是多此一舉(預設就有幹嘛加) 11/29 23:39
OPIV: 告訴編譯器它就是auto不要給我亂改 11/30 13:43