目 錄
1 讓自己習慣C++(View Yourself to C++) 1
1.1 條款1:視C++為一個語言聯邦(View C++ as a federation of language) 1
1.2 條款2:盡量使用consts,enums和inlines,少用#define (Prefer consts,enums and inlines to #define) 1
1.3 條款3:盡可能使用const(Use const whenever possible) 2
1.3.1 const與指針 2
1.3.2 const與疊代器 2
1.3.3 const與成員函數 3
1.4 條款4:關于“将對象初始化”這事,C++似乎反複無常(Make sure that object are initialized before they’re used) 3
2 構造、析構和指派運算(Constructors,Destructors and Assignment Operators) 5
2.1 條款5:了解C++預設編寫并調用哪些函數(Know what functions C++ silently writes and calls) 5
2.2 條款6:若不想使用編譯器自動生成的函數,就該明确拒絕(Explicitly disallow the use of compiler-generated function you do not want) 5
2.3 條款7:為多态基類聲明virtual析構函數(Declare destructors virtual in polymorphic base class) 6
2.4 條款8:别讓異常逃離析構函數(Prevent exception from leaving destructors) 7
2.5 條款9:絕不在構造和析構函數過程中調用virtual函數(Never call virtual function during construction or destruction) 9
2.6 條款10:令operator=傳回一個refernce to *this (Have assignment operators return a reference to *this) 9
2.7 條款11:在operator=中處理“自我指派”(Handle assignment to self in operator=) 9
2.8 條款12:複制對象時無妄其每一個成分(Copy all parts of an object) 11
3 資源管理(Resource Management) 13
3.1 條款13:以對象管理資源(Use object to manage resource) 13
3.2 條款14:在資源管理類中小心copying行為(Think carefully about copying behavior in resource-managing classes) 14
3.3 條款15:在資源管理類中提供對原始資源的通路(Provide access to raw resource in resource-managing classes) 15
3.4 條款16:成對使用new和delete時要采用相同形式(Use the same form in corresponding uses of new and delete) 15
3.5 條款17:以獨立語句将newed對象置于智能指針(Store newed objects in smart pointers in standalone statement) 16
4 設計與聲明 16
4.1 條款18:讓接口容易被正确使用,不易被誤用(Make interfaces easy to use correctly and hard to use incorrectly) 16
4.2 條款19:設計class猶如設計type(Treat class design as type design) 17
4.3 條款20:甯以pass-by-reference-to-const 替換pass-by-value (Prefer pass-by-reference-to-const to pass-by-value) 17
4.4 條款21:必須傳回對象時,别妄想傳回其reference(Don 't try to return a reference when you must return an object) 18
4.5 條款22:将成員變量聲明為private(Declare data members private) 20
4.6 條款23:甯以non-member、non-friend替換member函數 (Prefer non-member non-friend functions to member functions) 20
4.7 條款24:若所有參數皆需類型轉換,請為此采用non-member 函數(Declare non-member functions when type conversions should apply to all parameters) 21
4.8 條款25:考慮寫出一個不抛異常的swap函數 (Consider support for non-throwing swap) 23
5 實作(Implementation) 26
5.1 條款26:盡可能延後變量定義式的出現時間(Postpone variable definitions as long as possible) 26
5.2 條款27:盡量少做轉型動作 (Minimize casting) 27
5.3 條款28:避免傳回Handles指向對象内部成分 (Avoid returning “handles” to object internals) 30
5.4 條款29:為” 異常安全”而努力是值得的 (Strive for exception-safe code) 32
5.5 條款30:透切了解inlining的裡裡外外 (Understand the ins and outs of inlining) 34
5.6 條款31:将檔案間的編譯依存關系降至最低 (Minimize compilation dependencies between files) 35
6 繼承與面向對象(Inheritance and Object-Oriented Design) 37
6.1 條款32:确定你的public繼承塑模出is-a關系 (Make sure public inheritance models “is-a”) 37
6.2 條款33:避免遮掩繼承而來的名稱 (Avoid hiding inherited names) 38
6.3 條款34:區分借口繼承和實作繼承 (Differentiate between inheritance of interface and inheritance of implementation) 41
6.4 條款35:考慮virtual函數以外的其他選擇 (consider alternatives to virtual functions) 42
6.5 條款36:絕不重新定義繼承而來的non-virtual函數 (Never redefine a function 's inherited non-virtual function) 46
6.6 條款37:絕不重新定義繼承而來的預設參數值(Never redefine a function 's inherited default parameter value) 47
6.7 條款38:通過複合塑模出has-a或“根據某實物實作出” (Mode "has-a" or " is-implementation-in-terms-of" through composition) 48
6.8 條款39:明智而審慎地使用private繼承(Use private inheritance judiciously) 48
6.9 條款40:明智而審慎地使用多重繼承 (Use multiple inheritance judiciously) 50
7 模闆與泛型程式設計 (Templates and Generic Programming) 53
7.1 條款41:了解隐式接口和編譯期多态 (Understand implicit interface and compile-time polymorphism) 53
7.2 條款42:了解typename的雙重意義 (Understand the two meaning of typename) 54
7.3 條款43:學習處理模闆化基類内的名稱 (Know how to access names in templatized base class) 56
7.4 條款44:将與參數無關的代碼抽離templates (Factor parameter-independent code out of templates) 59
7.5 條款45:運用成員函數模闆介紹所有相容類型 (Use member function templates to accept "all compatible types") 61
7.6 條款46:需要類型轉換時請為目标定義非成員函數 (Define non-member functions inside templates when type conversions are desired) 62
7.7 條款47:請使用traits classes表現類型資訊 (Use traits classes for information about types) 64
7.8 條款48:認識template元程式設計 (Be aware of template metaprogramming) 67
8 定制new和delete(Customizing new and delete) 68
8.1 條款49:了解new-handler行為 (Understand the behavior of the new-handler) 68
8.2 條款50:了解new和delete的合理替換時機 (Understand when it makes sense to replace new and delete) 72
8.3 條款51:編寫new和delete時需要固守成規 (Be aware of template metaprogramming) 72
8.4 條款52:寫了placement new也要寫placement delete (Be aware of template metaprogramming) 74
9 其他(Others) 77
9.1 C++關鍵字explicit 77