[問題] Opaque pointer的應用
website: https://en.wikipedia.org/wiki/Opaque_pointer#C
上網查了一下,opaque pointer主要是用來hide implementation,也就是作了一個encap-
sulation的動作。
看了一下wiki給的C example,有幾個問題想請教各位
在例題裡,作者把structure definition放在obj.c,且include obj.h,並且在obj.h裡宣
告了 struct obj
Q1: 第一次看到struct obj;這種用法,這種用法是告訴compiler我在別的file裡有define
struct obj這個data type嗎? 如果是的話,那應該要 + extern吧?
為什麼不加extern也可以?
Q2: 剛剛翻了textbook(C how to program),作者說:
struct obj{
int id;
};
這個行為叫"define"一個structure且不reserve any space in memory(page 384)。
這個跟我心中認為的define有點出入,不reserve memory的話不是應該叫declare嗎?
還是對於struct與對於variable來說,define與declare的定義不一樣?
Q3: 我原本習慣的作法是直接把
struct obj{
int id;
};
這個definition放在obj.h。並不像作者,把它放在.c檔且在.h裡面多declare一個
struct obj。
我想這就是作者所謂的hide implementation。也就是說,struct裡面的member就是
implementation囉?
我認知的implementation是指"實作",也就是function裡的snippet。
所以struct裡面的member也算是implementation嗎?還是在特定的context裡才算數?
謝謝撥冗查看~
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.216.85.243
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1530241960.A.A2B.html
推
06/29 11:32,
7年前
, 1F
06/29 11:32, 1F
→
06/29 11:32,
7年前
, 2F
06/29 11:32, 2F
不好意思,請問這句是什麼意思?
→
06/29 11:33,
7年前
, 3F
06/29 11:33, 3F
→
06/29 11:34,
7年前
, 4F
06/29 11:34, 4F
→
06/29 12:39,
7年前
, 5F
06/29 12:39, 5F
→
06/29 12:41,
7年前
, 6F
06/29 12:41, 6F
我剛剛用gcc試了一下,就算沒加struct obj;這行宣告,只宣告
void obj_setid(struct obj *, int); compiler還是給過。我認為就算沒加struct obj;
對於obj_setid這個function來說,struct obj *本身就是一個declare了,並不需要在前
面多加struct obj;這行宣告
→
06/29 12:48,
7年前
, 7F
06/29 12:48, 7F
→
06/29 12:49,
7年前
, 8F
06/29 12:49, 8F
→
06/29 12:51,
7年前
, 9F
06/29 12:51, 9F
→
06/29 13:37,
7年前
, 10F
06/29 13:37, 10F
→
06/29 13:38,
7年前
, 11F
06/29 13:38, 11F
→
06/29 13:39,
7年前
, 12F
06/29 13:39, 12F
→
06/29 13:40,
7年前
, 13F
06/29 13:40, 13F
→
06/29 13:40,
7年前
, 14F
06/29 13:40, 14F
→
06/29 13:41,
7年前
, 15F
06/29 13:41, 15F
→
06/29 13:41,
7年前
, 16F
06/29 13:41, 16F
→
06/29 13:42,
7年前
, 17F
06/29 13:42, 17F
→
06/29 13:43,
7年前
, 18F
06/29 13:43, 18F
推
06/29 16:12,
7年前
, 19F
06/29 16:12, 19F
我發現就算沒宣告struct obj;這行,程式也編譯的過。
ideone: https://ideone.com/VbUs6R
※ 編輯: zzss2003 (60.251.49.183), 06/29/2018 17:52:41
※ 編輯: zzss2003 (60.251.49.183), 06/29/2018 17:53:30
→
06/29 18:53,
7年前
, 20F
06/29 18:53, 20F
→
06/29 18:54,
7年前
, 21F
06/29 18:54, 21F
推
06/29 20:38,
7年前
, 22F
06/29 20:38, 22F
→
06/29 20:42,
7年前
, 23F
06/29 20:42, 23F
推
06/29 21:08,
7年前
, 24F
06/29 21:08, 24F
→
06/29 21:09,
7年前
, 25F
06/29 21:09, 25F
→
06/30 02:39,
7年前
, 26F
06/30 02:39, 26F
→
06/30 02:39,
7年前
, 27F
06/30 02:39, 27F
→
06/30 02:46,
7年前
, 28F
06/30 02:46, 28F
→
06/30 02:46,
7年前
, 29F
06/30 02:46, 29F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章