譯序 vii 中英簡繁術(shù)語對照 ix 目錄 xvii 序言 xxi 致謝 xxiii 導讀 1 1. 讓自己習慣C++ 11 Accustoming Yourself to C++ 11 條款01:視C++ 為一個語言聯(lián)邦 11 View C++ as a federation of languages 11 條款02:盡量以const, enum, inline替換 #define 13 Prefer consts,enums, and inlines to #defines. 13 條款03:盡可能使用const 17 Use const whenever possible. 17 條款04:確定對象被使用前已先被初始化 26 Make sure that objects are initialized before they're used. 26 2. 構(gòu)造/析構(gòu)/賦值運算 34 Constructors, Destructors, and Assignment Operators 34 條款05:了解C++ 默默編寫并調(diào)用哪些函數(shù) 34 Know what functions C++ silently writes and calls. 34 條款06:若不想使用編譯器自動生成的函數(shù),就該明確拒絕 37 Explicitly disallow the use of compiler-generated functions you do not want. 37 條款07:為多態(tài)基類聲明virtual析構(gòu)函數(shù) 40 Declare destructors virtual in polymorphic base classes. 40
條款08:別讓異常逃離析構(gòu)函數(shù) 44 Prevent exceptions from leaving destructors. 44 條款09:絕不在構(gòu)造和析構(gòu)過程中調(diào)用virtual函數(shù) 48 Never call virtual functions during construction or destruction. 48 條款10:令operator= 返回一個reference to *this 52 Have assignment operators return a reference to *this. 52 條款11:在operator= 中處理“自我賦值” 53 Handle assignment to self in operator=. 53 條款12:復(fù)制對象時勿忘其每一個成分 57 Copy all parts of an object. 57 3. 資源管理 61 Resource Management 61 條款13:以對象管理資源 61 Use objects to manage resources. 61 條款14:在資源管理類中小心coping行為 66 Think carefully about copying behavior in resource-managing classes. 66 條款15:在資源管理類中提供對原始資源的訪問 69 Provide access to raw resources in resource-managing classes. 69 條款16:成對使用new和delete時要采取相同形式 73 Use the same form in corresponding uses of new and delete. 73 條款17:以獨立語句將newed對象置入智能指針 75 Store newed objects in smart pointers in standalone statements. 75 4. 設(shè)計與聲明 78 Designs and Declarations 78 條款18:讓接口容易被正確使用,不易被誤用 78 Make interfaces easy to use correctly and hard to use incorrectly. 78 條款19:設(shè)計class猶如設(shè)計type 84 Treat class design as type design. 84 條款20:寧以pass-by-reference-to-const替換pass-by-value 86 Prefer pass-by-reference-to-const to pass-by-value. 86 條款21:必須返回對象時,別妄想返回其reference 90 Don't try to return a reference when you must return an object. 90 條款22:將成員變量聲明為private 94 Declare data members private. 94 條款23:寧以non-member、non-friend替換member函數(shù) 98 Prefer non-member non-friend functions to member functions. 98 條款24:若所有參數(shù)皆需類型轉(zhuǎn)換,請為此采用non-member函數(shù) 102 Declare non-member functions when type conversions should apply to all parameters. 102
條款25:考慮寫出一個不拋異常的swap函數(shù) 106 Consider support for a non-throwing swap. 106 5. 實現(xiàn) 113 Implementations 113 條款26:盡可能延后變量定義式的出現(xiàn)時間 113 Postpone variable definitions as long as possible. 113 條款27:盡量少做轉(zhuǎn)型動作 116 Minimize casting. 116 條款28:避免返回handles指向?qū)ο髢?nèi)部成分 123 Avoid returning "handles" to object internals. 123 條款29:為“異常安全”而努力是值得的 127 Strive for exception-safe code. 127 條款30:透徹了解inlining的里里外外 134 Understand the ins and outs of inlining. 134 條款31:將文件間的編譯依存關(guān)系降至最低 140 Minimize compilation dependencies between files. 140 6. 繼承與面向?qū)ο笤O(shè)計 149 Inheritance and Object-Oriented Design 149 條款32:確定你的public繼承塑模出is-a關(guān)系 150 Make sure public inheritance models "is-a." 150 條款33:避免遮掩繼承而來的名稱 156 Avoid hiding inherited names. 156 條款34:區(qū)分接口繼承和實現(xiàn)繼承 161 Differentiate between inheritance of interface and inheritance of implementation. 161 條款35:考慮virtual函數(shù)以外的其他選擇 169 Consider alternatives to virtual functions. 169 條款36:絕不重新定義繼承而來的non-virtual函數(shù) 178 Never redefine an inherited non-virtual function. 178 條款37:絕不重新定義繼承而來的缺省參數(shù)值 180 Never redefine a function's inherited default parameter value. 180 條款38:通過復(fù)合塑模出has-a或"根據(jù)某物實現(xiàn)出" 184 Model "has-a" or "is-implemented-in-terms-of" through composition. 184 條款39:明智而審慎地使用private繼承 187 Use private inheritance judiciously. 187 條款40:明智而審慎地使用多重繼承 192 Use multiple inheritance judiciously. 192 7. 模板與泛型編程 199 Templates and Generic Programming 199
條款41:了解隱式接口和編譯期多態(tài) 199 Understand implicit interfaces and compile-time polymorphism. 199 條款42:了解typename的雙重意義 203 Understand the two meanings of typename. 203 條款43:學習處理模板化基類內(nèi)的名稱 207 Know how to access names in templatized base classes. 207 條款44:將與參數(shù)無關(guān)的代碼抽離templates 212 Factor parameter-independent code out of templates. 212 條款45:運用成員函數(shù)模板接受所有兼容類型 218 Use member function templates to accept "all compatible types." 218 條款46:需要類型轉(zhuǎn)換時請為模板定義非成員函數(shù) 222 Define non-member functions inside templates when type conversions are desired. 222 條款47:請使用traits classes表現(xiàn)類型信息 226 Use traits classes for information about types. 226 條款48:認識template元編程 233 Be aware of template metaprogramming. 233 8. 定制new和delete 239 Customizing new and delete 239 條款49:了解new-handler的行為 240 Understand the behavior of the new-handler. 240 條款50:了解new和delete的合理替換時機 247 Understand when it makes sense to replace new and delete. 247 條款51:編寫new和delete時需固守常規(guī) 252 Adhere to convention when writing new and delete. 252 條款52:寫了placement new也要寫placement delete 256 Write placement delete if you write placement new. 256 9. 雜項討論 262 Miscellany 262 條款53:不要輕忽編譯器的警告 262 Pay attention to compiler warnings. 262 條款54:讓自己熟悉包括TR1在內(nèi)的標準程序庫 263 Familiarize yourself with the standard library, including TR1. 263 條款55:讓自己熟悉Boost 269 Familiarize yourself with Boost. 269 A 本書之外 273 B 新舊版條款對映 277 索引 280