No description
AI Reading Assistant
Whole-book reading guide from stratified index samples; jump to passages in the text
AI guide
# Effective C++: 55 Specific Ways to Improve Your Programs and Designs (3rd Edition)
## 【One-Line Pitch】
The essential "second book" for any C++ developer, this classic distills 25 years of hard-won expertise into 55 concrete, actionable guidelines that bridge the gap between knowing C++ syntax and writing professional-grade, efficient, and maintainable code. If you've finished an introductory C++ course and want to think like an expert, this is your next read.
## 【Book Arc】
- **Opening (~0%–10%)**: The translator's preface and table of contents set the stage, framing the book as a "high-density accumulation of expert experience" — a collection of 55 independent "items" (条款) organized into 9 thematic chapters, from basic habits to advanced template metaprogramming.
- **Early (~10%–23%)**: Chapter 1 establishes foundational habits: viewing C++ as a federation of four sub-languages, preferring `const`, `enum`, and `inline` over `#define`, and the critical discipline of always initializing objects before use — including the member initialization list rule and the local-static-object pattern for cross-translation-unit initialization order.
- **Early (~23%–32%)**: Chapters 2–3 dive into the "big three" (construction, destruction, assignment) and resource management. Key insights include why virtual functions aren't virtual during base-class construction, the self-assignment/exception-safety connection in `operator=`, and the RAII principle with `shared_ptr` vs. `auto_ptr` trade-offs.
- **Middle (~32%–48%)**: Chapters 4–5 shift to design and declaration: making interfaces easy to use correctly and hard to misuse, avoiding object slicing via pass-by-reference, preferring non-member non-friend functions for encapsulation, and minimizing casts — with practical alternatives to `dynamic_cast`.
- **Late (~48%–100%)**: The remaining chapters cover advanced topics: templates and generic programming (including `typename`'s dual meaning, traits classes, and template metaprogramming), customizing `new`/`delete`, and final miscellany on compiler warnings, TR1, and Boost. The book closes with a new-vs-old edition item comparison table.
## 【Key Takeaways】
- **View C++ as a language federation** (Early): C++ is really four sub-languages — C, Object-Oriented C++, Template C++, and the STL — each with its own rules and idioms. Switching between them requires switching your mental model; this single insight explains many otherwise confusing behaviors.
- **Prefer the compiler over the preprocessor** (Early): Use `const`, `enum`, and `inline` instead of `#define` for constants and function-like macros. This gives you type safety, scoping, and debugger visibility — things the preprocessor can never provide.
- **Always initialize objects before use** (Early): C++ doesn't guarantee initialization for built-in types, and constructors must use member initialization lists (in declaration order) rather than assignment. For non-local static objects across translation units, replace them with local static objects to avoid initialization-order fiascoes.
- **Make copying functions complete and non-redundant** (Early): When writing copy constructors and copy assignment operators, copy *all* data members and *all* base-class components. Never have one copying function call the other — extract shared logic into a private `init` function instead.
- **Use RAII objects to manage resources** (Early): Acquire resources in constructors and release them in destructors. Prefer `tr1::shared_ptr` over `auto_ptr` because its copy behavior is intuitive; `auto_ptr`'s copy transfers ownership and nulls the source.
- **Design interfaces that are easy to use correctly and hard to misuse** (Middle): Consistency with built-in types, creating new types to constrain operations, and using `shared_ptr` with custom deleters (which can handle DLL issues and auto-unlock mutexes) all promote correct usage.
- **Prefer non-member non-friend functions for encapsulation** (Middle): Counter-intuitively, non-member functions increase encapsulation because fewer functions can access private data. This also improves packaging flexibility and reduces compilation dependencies.
- **Minimize casting — it breaks the type system** (Middle): C++ casts are more dangerous than in C/Java/C#. Prefer container-based designs with type-safe vectors over `dynamic_cast` chains; when you must cast, use the new-style casts and understand their costs.
## 【Reading Tips】
- **Skim the table of contents first** (~10%): The 55 items are independent, so treat this as a reference — read the chapters most relevant to your current work, not necessarily front-to-back.
- **Deep-read Chapters 1–3** (~10%–32%): These foundational items (initialization, copying, RAII) are the most universally applicable and will change how you write every class. Pay special attention to Item 4 (initialization) and Item 11 (self-assignment/exception safety).
- **Watch for the "Remember" boxes**: Each item ends with a bulleted summary — use these for quick review and as a checklist for code reviews.
- **Be patient with the template chapters** (~48%+): Items 42–48 on `typename`, traits, and template metaprogramming are the densest material. Skim them first, then return when you actually need these advanced techniques.
- **Pair with the companion volume**: The translator recommends *More Effective C++* as a follow-up — this third edition covers the modern language features, while the companion handles additional edge cases.
## 【Coverage Limits】
The excerpts cover the book's structure, foundational items (1–4, 5–6, 9–11, 13–15, 18, 20–24, 26–27), and the table of contents for later chapters, but do not include detailed content for items 28–52. Specific code examples for advanced topics (template metaprogramming, custom memory management) are not fully represented in this guide.
##
Page 12
有效率,又要有弹性,又要前瞻塑远,又要回溯相 容,又要治大国,又要烹小鲜,学习起来当然就不可能太简单。在庞大复杂的机制 下,万千使用者前仆后继的动力是:一旦学成,妙用无穷。 C++相关书籍车载斗覃,如天上繁星,如过江之渔卧广博如四库全书者有之(The C++ Programming Language、 C++ P...
View in text
Excerpt 2
Accustoming Yourself to C++ 不论你的编程背景是什么, C++ 都可能让你觉得有点儿熟悉。它是一个威力 强大的语言,带着众多特性,但是在你可以驾驭其威力并有效运用其特性之前,你 必须先习惯 C++的办事方式。本书谈的便是这个。总有某些东西比其他更基础些, 本章就是最基本的一些东西。 条款...
View in text
Excerpt 3
算将那些语法呈现给你看。单纯地接受这个 叙述吧:你不该令 copy assignment操作符调用 copy构造函数。 反方向一一令 copy构造函数调用 copy assignment操作符—一-同样无意义。构造 函数用来初始化新对象,而 assignment操作符只施行于已初始化对象身上。对一个 尚未构造好的...
View in text
Excerpt 4
ourth(l, 4); Ra已onal result; result= oneFourth * 2; //没问题 result= 2 * oneFourth; II万岁,通过编译了! 这当然是个快乐的结局,不过还有一点必须操心: opera七or* 是否应该成为 Rational class 的一个 frien...
View in text
Excerpt 5
are s; asser七 (s.wid七h () == s. height()) ; //这对所有正方形一定为真。 makeBigger(s); //由于继承, s 是一种 (is-a) 矩形, //所以我们可以增加其面积。 asser七 (s. width() == s.height()); //对所有正方形...
View in text
Excerpt 6
public File {...) ; class OutputFile: public File {...); class IOFile: public InputFile, public Ou七putFile 任何时候如果你有一个继承体系而其中某个base class 和某个 derived class 之间...
View in text
Excerpt 7
s 用来表 述独立的设计选项(所谓 “policies") ,然后可以任意结合它们,导致模式实现 品带着客户定制的行为。这项技术已被用来让若干 templates 实现出智能指针的 行为政策 (behavioral policies) ,用以在编译期间生成数以百计不同的智能指针 类型。这项技术已经超越编程工艺领域...
View in text
Excerpt 8
环形会造成每个对象的引用次数 都超过 0 即使指向这个环形的所有指针都已被销毁(也就是这一群对象整体 看来已无法触及)。这就是为什么又有个 trl: :weak_ptrs 的原因。 trl: :weak_p扛s 的设计使其表现像是“非环形 trl: : shared_ ptr-based 数据结 构”中的环形感生...
View in text
Tags
AI categories
ProgrammingC++Code
Text Preview (First 20 pages)
Registered users can read the full content for free
Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.
Generating text preview…
Loading comments...
Reply to Comment
Edit Comment