作者anoymouse (沒有暱稱)
看板C_and_CPP
標題[問題] 一般變數與const變數 在表頭檔問題
時間Sun Aug 2 18:39:28 2020
C++ / C
Windows10 Keil5
1.
個人認知header file 如果被一個source code includes,就是在那個
檔案進行Inline吧?
那如果在header file定義一個變數,不就只代表每個include 其header file的
檔案都有一個變數:同樣名稱同樣數值不同記憶體位址的一個個體嗎?
為什麼在linking的時候會出現重複定義的錯誤呢?
重複定義不是要在同一個檔案的scope出現兩次才算重複定義?
2.
const變數被定義在header file裡面的話,可以不消耗任何記憶體空間?
謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.136.222.105 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1596364770.A.D98.html
→ tomsawyer: 1.的狀況是不是用ifndef 若沒用extern一樣會遇到啊? 08/02 19:00
推 chuegou: 欸?keilC允許在.h宣告變數嗎?是我太久沒用還是記錯 08/02 19:56
→ Lipraxde: 應該剛好適合你 08/02 21:01
→ Lipraxde: 現在都有人整理好這種東西,真方便啊... 08/02 21:08
→ anoymouse: 感謝 看看先!! 08/02 22:07
→ galic: 1. static 2. C的const是read-only 不是概念上的常數 08/03 00:08
推 steve1012: header 可以用inline constexpr 08/03 03:14
→ Killercat: const不管放哪都一樣 都是在.rodata segment 08/03 10:14