內容大鋼
有人說C++程序員可以分為兩類,讀過Effective C++的和沒讀過的。世界頂級C++大師Scott Meyers成名之作的第三版的確當得起這樣的評價。當您讀過這本書之後,就獲得了迅速提升自己C++功力的一個契機。
在國際上,本書所引起的反響,波及整個電腦技術的出版領域,餘音至今未絕。幾乎在所有C++書籍的推薦名單上,本書都會位於前三名。作者高超的技術把握力、獨特的視角、詼諧輕鬆的寫作風格、獨具匠心的內容組織,都受到極大的推崇和仿效。這種奇特的現象,只能解釋為人們對這本書衷心的讚美和推崇。
這本書不是讀完一遍就可以束之高閣的快餐讀物,也不是用以解決手邊問題的參考手冊,而是需要您去反覆閱讀體會的,C++是真正程序員的語言,背後有著精深的思想與無以倫比的表達能力,這使得它具有類似宗教般的魅力。希望這本書能夠幫您跨越C++的重重險阻,領略高處才有的壯美風光,做一個成功而快樂的C++程序員。
目錄
譯序
中英簡繁術語對照
目錄
序言
致謝
導讀
1.讓自己習慣C++
Accustoming Yourself to C++
條款01:視C++ 為一個語言聯邦
View C++ as a federation of languages
條款02:盡量以const, enum, inline替換#define
Prefer consts,enums, and inlines to #defines
條款03:盡可能使用const
Use const whenever possible
條款04:確定對象被使用前已先被初始化
Make sure that objects are initialized before they're used
2.構造/析構/賦值運算
Constructors, Destructors, and Assignment Operators
條款05:了解C++ 默默編寫並調用哪些函數
Know what functions C++ silently writes and calls
條款06:若不想使用編譯器自動生成的函數,就該明確拒絕
Explicitly disallow the use of compiler-generated functions you do not want
條款07:為多態基類聲明virtual析構函數
Declare destructors virtual in polymorphic base classes
條款08:別讓異常逃離析構函數
Prevent exceptions from leaving destructors
條款09:絕不在構造和析構過程中調用virtual函數
Never call virtual functions during construction or destruction
條款10:令operator=返回一個reference to *this
Have assignment operators return a reference to *this
條款11:在operator=中處理「自我賦值」
Handle assignment to self in operator
條款12:複製對象時勿忘其每一個成分
Copy all parts of an object
3.資源管理
Resource Management
條款13:以對象管理資源
Use objects to manage resources
條款14:在資源管理類中小心copying行為
Think carefully about copying behavior in resource-managing classes
條款15:在資源管理類中提供對原始資源的訪問
Provide access to raw resources in resource-managing classes
條款16:成對使用new和delete時要採取相同形式
Use the same form in corresponding uses of new and delete
條款17:以獨立語句將newed對象置入智能指針
Store newed objects in smart pointers in standalone statements
4.設計與聲明
Designs and Declarations
條款18:讓介面容易被正確使用,不易被誤用
Make interfaces easy to use correctly and hard to use incorrectly
條款19:設計class猶如設計type
Treat class design as type design
條款20:寧以pass-by-reference-to-const替換pass-by-value
Prefer pass-by-reference-to-const to pass-by-value
條款21:必須返回對象時,別妄想返回其reference
Don't try to return a reference when you must return an object
條款22:將成員變數聲明為private
Declare data members private
條款23:寧以non-member、non-friend替換member函數
Prefer non-member non-friend functions to member functions
條款24:若所有參數皆需類型轉換,請為此採用non-member函數
Declare non-member functions when type conversions should apply to all parameters
條款25:考慮寫出一個不拋異常的swap函數
Consider support for a non-throwing swap
5.實現
Implementations
條款26:盡可能延後變數定義式的出現時間
Postpone variable definitions as long as possible
條款27:盡量少做轉型動作
Minimize casting
條款28:避免返回handles指向對象內部成分
Avoid returning "handles" to object internals
條款29:為「異常安全」而努力是值得的
Strive for exception-safe code
條款30:透徹了解inlining的里裡外外
Understand the ins and outs of inlining
條款31:將文件間的編譯依存關係降至最低
Minimize compilation dependencies between files
6.繼承與面向對象設計
Inheritance and Object-Oriented Design
條款32:確定你的public繼承塑模出is-a關係
Make sure public inheritance models "is-a."
條款33:避免遮掩繼承而來的名稱
Avoid hiding inherited names
條款34:區分介面繼承和實現繼承
Differentiate between inheritance of interface and inheritance of implementation
條款35:考慮virtual函數以外的其他選擇
Consider alternatives to virtual functions
條款36:絕不重新定義繼承而來的non-virtual函數
Never redefine an inherited non-virtual function
條款37:絕不重新定義繼承而來的預設參數值
Never redefine a function's inherited default parameter value
條款38:通過複合塑模出has-a或「根據某物實現出」
Model "has-a" or "is-implemented-in-terms-of" through composition
條款39:明智而審慎地使用private繼承
Use private inheritance judiciously
條款40:明智而審慎地使用多重繼承
Use multiple inheritance judiciously
7.模板與泛型編程
Templates and Generic Programming
條款41:了解隱式介面和編譯期多態
Understand implicit interfaces and compile-time polymorphism
條款42:了解typename的雙重意義
Understand the two meanings of typename
條款43:學習處理模板化基類內的名稱
Know how to access names in templatized base classes
條款44:將與參數無關的代碼抽離templates
Factor parameter-independent code out of templates
條款45:運用成員函數模板接受所有兼容類型
Use member function templates to accept "all compatible types."
條款46:需要類型轉換時請為模板定義非成員函數
Define non-member functions inside templates when type conversions are desired
條款47:請使用traits classes表現類型信息
Use traits classes for information about types
條款48:認識template元編程
Be aware of template metaprogramming
8.定製new和delete
Customizing new and delete
條款49:了解new-handler的行為
Understand the behavior of the new-handler
條款50:了解new和delete的合理替換時機
Understand when it makes sense to replace new and delete
條款51:編寫new和delete時需固守常規
Adhere to convention when writing new and delete
條款52:寫了placement new也要寫placement delete
Write placement delete if you write placement new
9.雜項討論
Miscellany
條款53:不要輕忽編譯器的警告
Pay attention to compiler warnings
條款54:讓自己熟悉包括TR1在內的標準程序庫
Familiarize yourself with the standard library, including TR1
條款55:讓自己熟悉Boost
Familiarize yourself with Boost
A 本書之外
B 新舊版條款對映
索引