|
Item1:C++ 就像一個聯邦政府(主語言),統合許多州(子語言)
包含
1. C , 建立在 C 語言的基礎上
2. Object-Oriented, 類別(classes), 封裝(encapsulation),繼承( inheritance), 多型(polymorphism), 虛擬函數(virtual function)
3. Template C++, 泛型設計(generic programming)
4. STL, template library, 包含 container, iterators, algorithms, and function objects.
summary:C++ is a federation of 4 sublanguages, each with its own conventions.
- seperate line -
item2: prefer consts, enums and inlines to #defines
因為 #define 會由 preprocessor 先處理,compiler 永遠看不見,當發生 error 時有時很難追蹤出錯誤來源。
summary: 1. for simple constants, prefer const objects or enums to #defines.
2. for function-like macros, prefer inline functions to #defines.
- seperate line -
item3: 盡可能用 const
用 const 可以幫助 compiler 偵測出使用上的錯誤
item4: 確保 objects 在使用之前已經初始化
manually initialize objects of built-in type, because C++ only sometimes initializes them itself.
in a constructor, prefer use the member initialization list to assignment inside the body of the constructor.
List data member in the initialization list in the same order they're declared in the class.
Avoid initialization order problems across translation units by replacing non-local static objects with local static objects. |
|