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