[問題] 物件的定義 .. function為何不是物件?
編譯器 : 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
04/05 12:43, 1F
→
04/05 12:43,
6年前
, 2F
04/05 12:43, 2F
→
04/05 12:47,
6年前
, 3F
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
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 2 篇):