[問題] 物件的定義 .. function為何不是物件?

看板C_and_CPP作者 (阿否)時間6年前 (2018/04/05 12:33), 編輯推噓1(106)
留言7則, 1人參與, 6年前最新討論串1/2 (看更多)
編譯器 : VC 13 問題(Question): 我在找關於"物件的定義"時候,有看到2014草案 內容是說 An object is a region of storage. [ Note: A function is not an object, regardless of whether or not it occupies storage in the way that objects do. —end note ] 這樣。 google之後,得到了這一篇: https://stackoverflow.com/questions/43971298/why-is-a-function-not-an-object 不過我實際嘗試之後,用int函式指標紀錄一個函式,然後用()轉成void、再轉回(int*)() 值是不變的? 當然回到本來的問題,連結內文提到: C++ requires that you can convert a pointer to any object type into a pointer to void,.. 略,就是指標轉過去在轉回來的值應該要是一樣的,這部份也是定義嗎? 原文下面證明函式不是物件就是用這個敘述去講,希望以下我沒理解錯 原文: That's not true with a pointer to a function though--if you take a pointer to a function, convert it to void *, and then convert it back to a pointer to a function, you may lose some information in the process. 我發文和測試的電腦是不同台,現在手邊電腦也沒辦法測試。 大致上就是用()硬轉型態去測。 在原本的電腦是可以編譯和執行的,然後四個輸出的結果會一樣。 本來整個問題就是"物件的定義",然後有看到 應該是在14草案的3.9.2-3 沒記錯位置的話,說到:參考、函式和void型態不是物件。 參考很明顯,void和函式就比較微妙了。 為了釐清觀念,才在板上發問這樣 謝謝各位。 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.223.65.196 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1522902817.A.688.html

04/05 12:43, 6年前 , 1F
對 C/C++ 來說, 函式指標和物件指標是兩種東西
04/05 12:43, 1F

04/05 12:43, 6年前 , 2F
而 void * 是一個物件指標, 拿它去指函式指標可能會有問題
04/05 12:43, 2F

04/05 12:47, 6年前 , 3F
至於之所以為什麼要分開, 你貼的那篇 SO 文的回答後半有提
04/05 12:47, 3F

04/05 12:48, 6年前 , 4F
是存在這種架構其中函式指標跟物件指標是完全不同的兩回事
04/05 12:48, 4F

04/05 12:48, 6年前 , 5F
(連指標大小都可能不一樣) 因此互轉的過程中就會掉東西
04/05 12:48, 5F

04/05 12:51, 6年前 , 6F
極端狀況例如哈佛架構裡程式和資料是物理上分開的
04/05 12:51, 6F

04/05 12:54, 6年前 , 7F
它們的指標可以很不一樣, 因此也就沒有互轉這回事
04/05 12:54, 7F
文章代碼(AID): #1QnQSXQ8 (C_and_CPP)
文章代碼(AID): #1QnQSXQ8 (C_and_CPP)