精華區beta Programming 關於我們 聯絡資訊
我有個問題,在C++的開發上請問.h檔要怎麼寫 以下我提供兩種STYLE ___________________________________________________________ |STYLE(I) | STYLE(II) | |---------------------------+-------------------------------| |//file:xxx9.h | //new file:xxx9.h | |#ifndef _XXX9_H | class A; | |#define _XXX9_H | class B; | | class A{/*...*/}; | class C; | | class B{/*...*/}; | #ifndef _XXX9_H | | class C{/*...*/}; | #define _XXX9_H | |#endif //_XXX9_H | class A{/*...*/}; | | | class B{/*...*/}; | | | class C{/*...*/}; | | | #endif //_XXX9_H | |------------------------------------------------------------ STYLE(I)是我常見的,但對STYLE(II)沒印象,不過似乎認為 STYLE(II)的寫法比較好,但我不知道我以後乾脆來改換STYLE(II)會不會有問題 小弟在此請教,謝謝^^ ========================================================== 為什麼會提到這個問題呢? 因為最近從STYLE(I)的開發經驗,想到說STYLE(II)比較方便於開發 如果完全不知道我在幹嘛的,為什麼要提出 STYLE(II), 則故事是下面這樣子的 (搞不好許多人都用STYLE(II)開發,只是我不曉得) =========================================================== 以前的寫法就好像C語言的寫法 //file : xxx9.h #ifndef _XXX9_H #define _XXX9_H class A{ /*...*/ }; class B{ /*...*/ }; class C{ /*...*/ }; #endif //_XXX9_H 但後來發現當這樣子的檔案一多時,由其像.h檔會互相include到 譬如我可能有另外的 ooo8.h這麼寫的 //file : ooo8.h #ifndef _OOO8_H #define _OOO8_H #include "rrr7.h" class D{ /*...*/ void show(B& theB_obj, A& theA_obj); }; #endif //_OOO8_H 因為 rrr7.h裡面本身又有include到其它.h檔,這使得在 file ooo8.h裡面的 class D是可以宣告一memeber function,使用 class B。 其實我在搞不太清楚的情況下 像有時候 就會因為這個 ooo8.h 裡面宣告了這樣子的member function而不會過 所以我把上面的 ooo8.h 改寫成如下 //new file : ooo8.h #ifndef _OOO8_H #define _OOO8_H #include "rrr7.h" class A; class B; class D{ /*...*/ void show(B& theB_obj, A& theA_obj); }; #endif //_OOO8_H 這新的 ooo8.h 檔,便可以讓compile通過 因此我推論之前會錯的原因是因為,我定義該member function 'show(A,B)' 之前因為沒宣告class A; 跟 class B; 所以才會錯。 後來我發現常常這種錯會讓自己搞不清楚錯誤在那邊, 於是我就想了一個方法,就是去改寫 xxx9.h //new file : xxx9.h class A; class B; class C; #ifndef _XXX9_H #define _XXX9_H class A{ /*...*/ }; class B{ /*...*/ }; class C{ /*...*/ }; #endif //_XXX9_H 我發現在使用xxx9.h 這樣子的style寫法 確實可以讓 舊的ooo8.h檔可以順利compile通過 也就是說 ooo8.h檔不用去再另外宣告像 class A; 或 class B;這樣子的東西了。 所以我想說,在C++裡,寫.h檔的時候 對於那些.h檔的寫法 我由 STYLE(I) //file : xxx9.h #ifndef _XXX9_H #define _XXX9_H class A{ /*...*/ }; class B{ /*...*/ }; class C{ /*...*/ }; #endif //_XXX9_H 這樣子的寫法 改換成像下面這樣子的寫法… (我把下面這寫法叫 STYLE(II) ) //new file : xxx9.h class A; class B; class C; #ifndef _XXX9_H #define _XXX9_H class A{ /*...*/ }; class B{ /*...*/ }; class C{ /*...*/ }; #endif //_XXX9_H 這樣子就似乎好管理多了。 但是我不知道如果使用這樣子的style會不會有問題, 因為我看過人家所開發的code,好像大多數是用STYLE(1)的方式 那麼我想要問一下,若我使用 STYLE(II)的方式到底可不可行 還是說會有什麼問題呢? (但搞不好C++的.h檔都是用STYLE(II)在寫的,只是我知識不足><) 先謝謝大家了^___________^y -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.114.71.92
ericwang1017:可以去看EXception c++ 裡面有一章 61.31.166.110 12/22 00:38
ericwang1017:教你如何脫偶 61.31.166.110 12/22 00:38
milochen:請問一下,脫偶的英文是什麼,謝謝^^ 125.225.145.74 12/22 22:05