Share E-Book
Scan to open this page

Scan with your phone to open this page

Author: Rich Yonts

Rating No ratings yet

Inside 100 C++ Mistakes and How To Avoid Them you’ll learn how to: • Design solid classes • Minimize resource allocation/deallocation issues • Use new C++ features • Identify the differences between compile and runtime issues • Recognize C-style idioms that miss C++ functionality • Use exceptions well 100 C++ Mistakes and How To Avoid Them gives you practical insights and techniques to improve your C++ coding kung fu. Author Rich Yonts has been using C++ since its invention in the 1980s. This book distills that experience into practical, reusable advice on how C++ programmers at any skill

AI Reading Assistant

Whole-book reading guide from stratified index samples; jump to passages in the text

AI guide
【One-Line Pitch】 A practical, mistake-driven tour of modern C++ that helps working developers write safer, clearer code in real (often legacy) codebases. Best for programmers who already know some C++ and want to level up their judgment rather than learn syntax from scratch. 【Book Arc】 - **Opening (~0%–10%)**: Frames the book's philosophy — correctness, readability, and effectiveness — and warns that undefined behavior is the enemy. Introduces the reader as someone likely working in large legacy codebases under team constraints. - **Early (~10%–32%)**: Covers class design and data types: smart pointers and ownership, `=default`/`=delete`, in-class initializers, uniform initialization, emplacement, structured bindings, scoped enums, and variable scoping. - **Middle (~32%–50%)**: Moves into resource management and standard-library usage: replacing C-style `FILE` and `malloc`/`free`, avoiding varargs, preferring vectors over arrays, and understanding output buffering. - **Late (~50%–70%)**: Tackles class invariants, constructors, and initialization pitfalls — including default constructors, primitive member initialization, and value validation. - **Ending (~70%–100%)**: Addresses advanced class operations: shallow vs. deep copy, base-class operators, virtual destructors, virtual calls in constructors/destructors, polymorphic arrays, variable shadowing, return value optimization, copy assignment, self-assignment, and implicit conversions. 【Key Takeaways】 - **Undefined behavior is the cardinal sin** (Opening): the book treats UB as something to avoid at all costs, since it can crash, corrupt, or silently misbehave — and compilers may optimize it away. - **Modern C++ features replace C-style idioms** (Early): scoped enums, smart pointers, `=default`/`=delete`, and uniform initialization all reduce error surface compared to older patterns. - **Ownership must be explicit** (Early): exclusive ownership via `unique_ptr` and shared ownership via `shared_ptr` prevent double-deletes and leaks; watch for circular references with shared pointers. - **Prefer standard containers and emplacement** (Early–Middle): vectors and emplace operations avoid unnecessary temporaries and manual memory management. - **Class invariants depend on constructors** (Late): failing to initialize every member — especially primitives — leaves objects in undefined states that the compiler won't catch. - **Polymorphism has sharp edges** (Ending): virtual destructors are mandatory in polymorphic bases; calling virtual functions in constructors/destructors and using polymorphic arrays are common traps. - **Copy semantics deserve careful attention** (Ending): shallow vs. deep copy, self-assignment, and returning references from copy assignment are recurring sources of bugs. - **Readability is a first-class concern** (throughout): limiting variable scope, initializing at declaration, and communicating intent with C++-style casts all reduce cognitive load. 【Reading Tips】 - Treat the book as a reference: jump to mistakes matching your current codebase pain points rather than reading linearly. - Deep-read the smart pointer and class design chapters (Early) — they underpin many later mistakes. - Skim the C-style idiom chapters if you already avoid `malloc`/`FILE`/varargs, but note the reasoning for legacy code work. - Pay special attention to the "SEE ALSO" cross-references; they connect related mistakes and reinforce patterns. - Keep a mental checklist of the three lenses — correctness, readability, effectiveness — and apply them to your own code as you read. 【Coverage Limits】 The excerpts cover the book's structure and many specific mistakes but do not include full code listings or every chapter's complete content. Some later chapters and detailed solutions are only partially represented.
Excerpt 1
developing new features and resolving existing problems in a code base of thousands or millions of lines of older code. Your challenge will be to learn how t...
View in text
Excerpt 2
to another using a shared pointer, and vice versa. A shared pointer keeps track of the number of references to its shared entity. The count increases for eac...
View in text
Excerpt 3
] > max) max = values[pos]; return max; } int main() { std::vector<int> values; values.push_back(1); values.push_back(-2); values.push_back(-3); std::cout <<...
View in text
Excerpt 4
e 57 for a discussion about virtual base class destructors. See Mistake 59 for a discussion of the pitfalls associated with slicing elements in a container....
View in text
Excerpt 5
range. Ideally, throw exceptions when the value is invalid. This is often the best approach, unless project leadership has determined that exceptions should...
View in text
Excerpt 6
ch is correct or that there are no memory problems lurking. Understand that even sophisticated tools will not detect all problems; valgrind is a great tool,...
View in text
Excerpt 7
function cannot possibly continue, it must signal an error. If the function is designed to handle empty lists (a NULL pointer), it is not an error and should...
View in text
Excerpt 8
if modern is unavailable) smart pointers wherever possible. Replace auto_ptr instances with at least Boost smart pointers wherever possible; much better are...
View in text
Tags
AI categories
C++ProgrammingSoftware
Publish Year: 2025
Language: English
Pages: 631
File Format: PDF
File Size: 2.7 MB
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…