看板 C_and_CPP 關於我們 聯絡資訊
最近在study谷歌的C++ Style Guide http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml 有一些看不太懂的地方: ===== Q1 ===== The -inl.h Files: ...However, implementation code properly belongs in .cc files, and we do not like to have much actual code in .h files unless there is a readability or performance advantage... 有聽過類似的建議,不要再header file裡面放太多實作, 實作盡量放在.c file裡,但是不太知道這樣做的好處是什麼??? 谷歌的style guide又提到"unless there is a readability or performance advantage" 但在甚麼樣的情況下,把實作的code搬到header file會提升performance呢??? ===== Q2 ===== Casting: Use C++ casts like static_cast<>(). Do not use other cast formats like int y = (int)x; or int y = int(x);. Pros: The problem with C casts is the ambiguity of the operation; sometimes you are doing a conversion (e.g., (int)3.5) and sometimes you are doing a cast (e.g., (int)"hello"); C++ casts avoid this. Additionally C++ casts are more visible when searching for them. 看不太懂C style的轉型不好的點在哪?? 意思是說C style的轉型太暴力, 連不合乎邏輯的轉型都可以轉的過嗎??? (比方說: (int)"hello") -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 122.116.153.199 ※ 編輯: rosemary0401 來自: 122.116.153.199 (11/18 17:30)
kevingwn:因為實作常變動,include該header的人每次更動都要重新編 11/18 17:37
kevingwn:譯 反之,如果header不變只要重新連結就可以了 11/18 17:38
kevingwn:例外狀況..大概是一些macro,inline或template function吧 11/18 17:41
kevingwn:轉型這個是安全問題,(int)x,除非x的型別可以正確轉成int 11/18 17:45
kevingwn:否則就是不安全的操作,用C++的cast可以明確的告訴編譯器 11/18 17:46
kevingwn:你想幹嘛,這樣它可以幫你檢查是否安全,C的轉型就不行了 11/18 17:48
legnaleurc:寫在 header 有時還有 linkage 的問題 11/18 21:24
legnaleurc:另外 xxx_cast 比較好搜尋 11/18 21:25
MOONRAKER:第一條不是所有C語言課本都會講的事情。 11/18 23:50
loveme00835:多個問題請整理在一篇發問, 勿再多開新討論串 11/19 06:19