[問題] static + extern

看板C_and_CPP (C/C++)作者 (沒有存在感的人)時間10年前 (2015/11/29 21:19), 編輯推噓2(2011)
留言13則, 3人參與, 最新討論串1/1
問題(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

11/29 21:42, , 1F
沒有 static extern 這種東西…
11/29 21:42, 1F

11/29 21:44, , 2F
但是你可以 static int foo; void bar(void) { extern int f
11/29 21:44, 2F

11/29 21:44, , 3F
oo; }
11/29 21:44, 3F

11/29 21:45, , 4F
在 scope 內繼續使用 scope 外的變數
11/29 21:45, 4F

11/29 21:48, , 5F
同樣的,也沒有 static auto variable
11/29 21:48, 5F

11/29 21:48, , 6F
如果你這樣 static int foo; void bar(void) { auto int foo
11/29 21:48, 6F

11/29 21:49, , 7F
; }
11/29 21:49, 7F

11/29 21:50, , 8F
那麼代表在 scope 內覆蓋掉 global 的宣告,宣告一個新的 au
11/29 21:50, 8F

11/29 21:50, , 9F
to 變數
11/29 21:50, 9F

11/29 22:34, , 10F
static auto只在C++11之後才生效,C++11之前是不合法的
11/29 22:34, 10F

11/29 22:57, , 11F
不過c++11的auto已經不只是儲存等級就是了
11/29 22:57, 11F

11/29 23:39, , 12F
c++11 之前 auto給我的感覺是多此一舉(預設就有幹嘛加)
11/29 23:39, 12F

11/30 13:43, , 13F
告訴編譯器它就是auto不要給我亂改
11/30 13:43, 13F
文章代碼(AID): #1MMljjao (C_and_CPP)
文章代碼(AID): #1MMljjao (C_and_CPP)