[問題]const的使用

看板C_and_CPP作者 (Jay)時間7年前 (2018/11/01 11:09), 7年前編輯推噓2(202)
留言4則, 2人參與, 7年前最新討論串1/1
開發平台(Platform): (Ex: Win10, Linux, ...) WIN10 編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出) VC++ 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 餵入的資料(Input): 預期的正確結果(Expected Output): 錯誤結果(Wrong Output): 程式碼(Code):(請善用置底文網頁, 記得排版,禁止使用圖檔) A.cpp double* m_Matrix; m_Matrix = new double[16]; const double *t_refer = m_Matrix; //指向常數的指標 GetMode(t_refer); B.cpp double* m_SecMatrix; m_SecMatrix = new double[16]; GetMode(const double* &m_matrix) { m_matrix = m_SecMatrix; } 補充說明(Supplement): 想請問各位前輩這個程式片段 指向常數的指標改變指標是合法的 為什麼B.cpp 裡要用 & 的符號呢? -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 59.127.200.146 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1541041767.A.A81.html

11/01 11:18, 7年前 , 1F
&和你要問的問題無關吧 沒&傳進來的是double的位置 有&
11/01 11:18, 1F

11/01 11:18, 7年前 , 2F
傳進來的才是那個指標變數
11/01 11:18, 2F
前輩解釋後 我突然意識到是傳參考呼叫 因為加上了CONST 搞得我有點混亂 謝謝前輩 ※ 編輯: asdfg1597860 (59.127.200.146), 11/01/2018 12:01:02

11/05 04:09, 7年前 , 3F
double *const ptr 跟 const double *ptr
11/05 04:09, 3F

11/05 04:10, 7年前 , 4F
他是指向常數變數的指標 不是指向變數的常數指標
11/05 04:10, 4F
文章代碼(AID): #1Rscvdg1 (C_and_CPP)