譯序(侯捷) v
目錄(Contents) vii
前言(Preface) xiii
致謝(Acknowledgments. 中文版略) xvii
導讀(Introduction) 001
改變舊有的C習慣(Shifting from C to C++) 013
條款1:盡量以 const 和 inline 取代 #define 013
Prefer const and inline to #define.
條款2:盡量以 取代 017
Prefer to .
條款3:盡量以 new 和 delete 取代 malloc() 和 free() 019
Prefer new and delete to malloc and free.
條款4:盡量使用 C++ 風格的注釋形式 021
Prefer C++-style comments.
內存管理(Memory Management) 022
條款5:使用相同形式的 new 和 delete 023
Use the same form in corresponding uses of new and delete.
條款6:記得在 destructor 中以delete 對付 pointer members 024
Use delete on pointer members in destructors.
條款7:為內存不足的狀況預做準備 025
Be prepared for out-of-memory conditions.
條款8:撰寫 operator new 和 operator delete 時應遵行公約 033
Adhere to convention when writing operator new and operator delete.
條款9:避免遮掩了 new 的正規(guī)形式 037
Avoid hiding the "normal" form of new.
條款10:如果你寫了一個operator new,
請對應也寫一個operator delete 039
Write operator delete if you write operator new.
構造函數、析構函數和Assignment 運算符 049
Constructors, Destructors, and Assignment Operators
條款11:如果classes內動態(tài)配置有內存,請為此 class 宣告
一個 copy constructor 和一個 assignment 運算符 049
Declare a copy constructor and an assignment operator
for classes with dynamically allocated memory.
條款12:在 constructor 中盡量以 initialization 取代 assignment 052
Prefer initialization to assignment in constructors.
條款13:Initialization list 中的 members 初始化排列次序應該
和其在 class 內的聲明次序相同 057
List members in an initialization list in the order
in which they are declared.
條款14:總是讓 base class 擁有 virtual destructor 059
Make sure base classes have virtual destructors.
條款15:令 operator= 傳回"*this 的 reference" 064
Have operator= return a reference to *this.
條款16:在 operator= 中為所有的 data members 賦予內容 068
Assign to all data members in operator=.
條款17:在 operator= 中檢查是否"自己賦值給自己" 071
Check for assignment to self in operator=.
類與函數之設計和聲明 077
Classes and Functions: Design and Declaration
條款18:努力讓接口完滿且最小化 079
Strive for class interfaces that are complete and minimal.
條款19:區(qū)分member functions, non-member functions和
friend functions三者 084
Differentiate among member functions, non-member functions,
and friend functions.
條款20:避免將 data members 放在公開接口中 089
Avoid data members in the public interface.
條款21:盡可能使用 const 091
Use const whenever possible.
條款22:盡量使用 pass-by-reference(傳址),
少用 pass-by-value(傳值) 098
Prefer pass-by-reference to pass-by-value.
條款23:當你必須傳回object 時,不要嘗試傳回 reference 101
Don’t try to return a reference when you must return an object.
條款24:在函數重載(function overloading)和
參數缺省化(parameter defaulting)之間,謹慎抉擇 106
Choose carefully between function overloading
and parameter defaulting.
條款25:避免對指針型別和數值型別進行重載 109
Avoid overloading on a pointer and a numerical type.
條款26:防衛(wèi)潛伏的 ambiguity(模棱兩可)狀態(tài) 113
Guard against potential ambiguity.
條款27:如果不想使用編譯器暗自產生的 member functions,
就應該明白拒絕它 116
Explicitly disallow use of implicitly generated member functions
you don’t want.
條款28:嘗試切割 global namespace(全域命名空間) 117
Partition the global namespace.
類與函數之實現 123
Classes and Functions: Implementation
條款29:避免傳回內部數據的 handles 123
Avoid returning "handles" to internal data.
條款30:避免寫出member function,傳回一個 non-const pointer 或
reference 并以之指向較低存取層級的 members 129
Avoid member functions that return non-const pointers or
references to members less accessible than themselves.
條款31:千萬不要傳回"函數內 local 對象的 reference",
或是"函數內以 new 獲得的指針所指的對象" 131
Never return a reference to a local object or to
a dereferenced pointer initialized by new within the function.
條款32:盡可能延緩變量定義式的出現 135
Postpone variable definitions as long as possible.
條款33:明智地運用 inlining 137
Use inlining judiciously.
條款34:將文件之間的編譯依賴關系(compilation dependencies)
降至最低 143
Minimize compilation dependencies between files.
繼承機制與面向對象設計 153
Inheritance and Object-Oriented Design
條款35:確定你的 public inheritance 模塑出 "isa" 的關系 154
Make sure public inheritance models "isa."
條款36:區(qū)分"接口繼承(interface inheritance)"和
"實現繼承(implementation inheritance)" 161
Differentiate between inheritance of interface and
inheritance of implementation.
條款37:絕對不要重新定義一個繼承而來的非虛擬函數 169
Never redefine an inherited nonvirtual function.
條款38:絕對不要重新定義一個繼承而來的缺省參數值 171
Never redefine an inherited default parameter value.
條款39:避免在繼承體系中做 cast down(向下轉型)的動作 173
Avoid casts down the inheritance hierarchy.
條款40:通過 layering(分層技術)來模塑 has-a 或
is-implemented-in-terms-of 的關系 182
Model "has-a" or "is-implemented-in-terms-of" through layering.
條款41:區(qū)分 inheritance 和 templates 185
Differentiate between inheritance and templates.
條款42:明智地運用 private inheritance(私有繼承) 189
Use private inheritance judiciously.
條款43:明智地運用多繼承(multiple inheritance,MI) 194
Use multiple inheritance judiciously.
條款44:說出你的意思并了解你所說的每一句話 210
Say what you mean; understand what you’re saying.雜項討論(Miscellany) 212
條款45:知道 C++(編譯器)默默為我們完成和調用哪些函數 212
Know what functions C++ silently writes and calls.
條款46:寧愿編譯和連接時出錯,也不要執(zhí)行時才錯 216
Prefer compile-time and link-time errors to runtime errors. 219
Ensure that non-local static objects are initialized
before they’re used.
條款48:不要對編譯器的警告訊息視而不見 223
Pay attention to compiler warnings.
條款49:盡量讓自己熟悉 C++ 標準程序庫 224
Familiarize yourself with the standard library.
條款50:加強自己對 C++ 的了解 232
Improve your understanding of C++.