2007年10月25日 星期四

[note] Effective C++ 3/e (5)

Item 32:Make sure public inheritance model is-a
public 繼承表示著 is-a 的關係。

Item 33:Avoid hiding inherited names
不遮避繼承而來的名稱。

Item 34:Differentiate between inheritance of interface and inheritance of implementation
適當地利用 virtual function 來達到介面繼承與實作繼承的關係,並區別兩種的不同。

Item 35:Consider alternatives to virtual functions
考慮以其它方式 (例如 NVI 或 Strategy pattern ) 來取代 virtual function。

Item 36:Never redefine an inherited non-virtual function
重新定義繼承而來的 non-virtual function 永遠是錯的。

Item 37:Never redefine a functions's inherited default parameter value
不重新定義繼承而來的預設參數值。

Item 38:Model has-a of implemented-in-term-of through composition
利用 composition 製造出 has-a 的關係。

Item 39:Use private inheritance judiciously
審慎決定使用 private 繼承 (private 繼承的層級通常比 composition 低)

Item 40:Use multiple inheritance judiciously
當鑽石型多重繼承時,請審慎使用虛擬繼承 (virtual inheritance)。

2007年10月16日 星期二

[note] Effective C++ 3/e (4)

Item 26:Postpone variable definitions as long as possible
變數要用時才宣告,不僅提高可讀性,也可以避不必要的成本。

Item 27:Minimize casting
可以不轉型就不轉型。

Item 28:Avoid returning handles to object internals
避免傳回一個 pointer 或 reference 指向物件裡的成員。

Item 29:Strive for exception-safe code
確保處理 exceptions 的 code 不會有任何問題。

Item 30:Understand the ins and outs of inlining
不要亂用 inline。

Item 31: Minimize compilation dependencies between files
降低編譯時檔案間的相依關係(使用factory functions)。

[note] Effective C++ 3/e (3)

Item 18:Make interface easy to use correctly and hard to use incorrectly
提供簡單明確,而且不易被誤用的介面。

Item 19:Treat class design as type design
設計 class 就如同設計一個 type。

Item 20:Prefer pass-by-reference-to-const to pass-by-value
傳遞參數給函式時,請使用 pass-by-reference-to-const 來代替 pass-by-value

Item 21:Do not try to return a reference when you must return an object
當必須回傳一個物件時,避免傳回 reference

Item 22:Declare data membes private
把 data member 都宣告成 private,以確保封裝性。

Item 23:Prefer non-member non-friend functions to member functions
為避免破壞封裝性,可以適當使用 non-member non-friend functions 來取代 member functions

Item 24:Prefer non-member functions when type conversions should apply to all parameters
若參數皆需要型別轉換,最好寫成 non-member functions (尤其對於 operator overloading)

Item 25: Consider support for a non-throwing swap
當你特化或重載 swap 函式時,別讓它丟出 exceptions

2007年10月14日 星期日

[note] Effective C++ 3/e (2)

Item 13:Use object to manage resources
利用 constuctors 及 destructors 被呼叫的時機這特性,來幫忙管理資源。

Item 14:Think carefully about copying behavior in resource-managing classes
在資源管理類別中,要注意 copy 的行為是否會導致什麼問題。

Item 15:Provide access to raw resources in resource-managing classes
使用物件(類別)來管理資源時,也要提供界面來存取原始資源。

Item 16:Use the same form in corresponding uses of new and delete
使用什麼型式的 new,就要使用什麼樣型式的 delete。

Item 17: Store newed objects in smart pointers in standalone statements
將 new 出來的物件放入智慧型指標。

2007年10月12日 星期五

[note] Effective C++ 3/e (1)

雖然以前已經把 Effective C++ 2/e 看完了,但那本書的出版時間已經是六年前了,上週逛天瓏就順手再買了 Effective C++ 3/e,翻了一下,內容上第三版與第二版其實差異不大,主要還是在談論一些 C++ 的開發經驗,以及一些重要而且常會犯錯誤,並且刪除了一些過時的 item,增加了一些新的東西,如 Boost 及 multi-threads 的開發注意事項。許多 C++ 開發者給 Effective C++ 的評論都是:沒有讀過 Effective C++ 及 More Effective C++ 的話,別說你懂 C++。

接著就慢慢整理每個Item的重點,雖然很多觀念在 2/e 都講到爛了,不過還是做個筆記吧:

Item 1:View C++ as a feduration of language
C++是由四個次語言所組成:C,Object-Oriented C++,Template C++,STL。

Item 2:Prefer const, enums and inline to #define
除非必要,否則儘量使用 const, enums 還有 inline 來取代 define,

Item 3:Use const whenever possible
儘可能使用 const (常數,函式及operators)

Item 4:Make sure that objects are initialized before they're used
確保每個物件在使用前已經initialized

Item 5:Know what functions C++ silently writes and calls
了解 default constructors,default copy constructors 及 default assignment operator 如何被產生

Item 6: Explicitly disallow the use of compiler-generated functions you do not want
若不想使用 compiler 產生的東西,就想辦法拒絕 (宣告成private,只宣告,不定義 )

Item 7:Declare destructors virtual in polymorphic base classes
若你的繼承結構中有 virtual functions 的話,記得加上一個 virtual destructors

Item 8:Prevent exceptions form leaving destructors

在destructors中放入exceptions時,要注意是否完備 (在任何情形下,資源皆會被釋放)



Item 9:Never call virtual functions during constructors or destructors
別在 constructors 或 destructors 中呼叫 virtual functions

Item 10:Have assignment operators return a reference to *this
讓 assignment operators 回傳 *this

Item 11:Handle assignment to self in operator=
在assignment operator overloading 中,記得處理 a=a 的情形

Item 12:Copy all parts of an object
在做物件的 copy 時,記得把所有部分都 copy 好 (像是內含 pointer 的物件 )

to be continue...