天天看點

Effective C++目錄

導讀 1

1.  讓自己習慣 C++ 11

Accustoming Yourself to C++ 11

條款 01 :視 C++  為一個語言聯邦 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.  構造 / 析構 / 指派運算 34

Constructors, Destructors, and Assignment Operators 34

條款 05 :了解 C++  默默編寫并調用哪些函數 34

Know what functions C++ silently writes and calls. 34

條款 06 :若不想使用編譯器自動生成的函數,就該明确拒絕 37

Explicitly disallow the use of compiler-generated functions you do not want. 37

條款 07 :為多态基類聲明 virtual 析構函數 40

Declare destructors virtual in polymorphic base classes. 40

條款 08 :别讓異常逃離析構函數 44

Prevent exceptions from leaving destructors. 44

條款 09 :絕不在構造和析構過程中調用 virtual 函數 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 :複制對象時勿忘其每一個成分 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.  設計與聲明 78

Designs and Declarations 78

條款 18 :讓接口容易被正确使用,不易被誤用 78

Make interfaces easy to use correctly and hard to use incorrectly. 78

條款 19 :設計 class 猶如設計 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 函數 98

Prefer non-member non-friend functions to member functions. 98

條款 24 :若所有參數皆需類型轉換,請為此采用 non-member 函數 102

Declare non-member functions when type conversions should apply to all parameters. 102

條款 25 :考慮寫出一個不抛異常的 swap 函數 106

Consider support for a non-throwing swap. 106

5.  實作 113

Implementations 113

條款 26 :盡可能延後變量定義式的出現時間 113

Postpone variable definitions as long as possible. 113

條款 27 :盡量少做轉型動作 116

Minimize casting. 116

條款 28 :避免傳回 handles 指向對象内部成分 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 :将檔案間的編譯依存關系降至最低 140

Minimize compilation dependencies between files. 140

6.  繼承與面向對象設計 149

Inheritance and Object-Oriented Design 149

條款 32 :确定你的 public 繼承塑模出 is-a 關系 150

Make sure public inheritance models "is-a." 150

條款 33 :避免遮掩繼承而來的名稱 156

Avoid hiding inherited names. 156

條款 34 :區分接口繼承和實作繼承 161

Differentiate between inheritance of interface and inheritance of implementation. 161

條款 35 :考慮 virtual 函數以外的其他選擇 169

Consider alternatives to virtual functions. 169

條款 36 :絕不重新定義繼承而來的 non-virtual 函數 178

Never redefine an inherited non-virtual function. 178

條款 37 :絕不重新定義繼承而來的預設參數值 180

Never redefine a function's inherited default parameter value. 180

條款 38 :通過複合塑模出 has-a 或 " 根據某物實作出 " 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 :了解隐式接口和編譯期多态 199

Understand implicit interfaces and compile-time polymorphism. 199

條款 42 :了解 typename 的雙重意義 203

Understand the two meanings of typename. 203

條款 43 :學習處理模闆化基類内的名稱 207

Know how to access names in templatized base classes. 207

條款 44 :将與參數無關的代碼抽離 templates 212

Factor parameter-independent code out of templates. 212

條款 45 :運用成員函數模闆接受所有相容類型 218

Use member function templates to accept "all compatible types." 218

條款 46 :需要類型轉換時請為模闆定義非成員函數 222

Define non-member functions inside templates when type conversions are desired. 222

條款 47 :請使用 traits classes 表現類型資訊 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 時需固守正常 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 在内的标準程式庫 263

Familiarize yourself with the standard library, including TR1. 263

條款 55 :讓自己熟悉 Boost 269

Familiarize yourself with Boost. 269 

繼續閱讀