Effective C++ Digital Collection 140 Ways to Improve Your Programming (Scott Meyers)(Z-Library)
Programming
Scott Meyers’s seminal C++ books– Effective C++ , More Effective C++ , and Effective STL –have been immensely helpful to hundreds of thousands of C++ programmers. All three are finally available together in this eBook collection.
5
Views
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.
Page
1
(This page has no text content)
Page
2
Effective C++ Digital Collection 140 Ways to Improve Your Programming Scott Meyers Upper Saddle River, NJ • Boston • Indianapolis • San Francisco New York • Toronto • Montreal • London • Munich • Paris • Madrid Capetown • Sydney • Tokyo • Singapore • Mexico City
Page
3
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals. The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests. For more information, please contact: U.S. Corporate and Government Sales (800) 382-3419 corpsales@pearsontechgroup.com For sales outside the United States, please contact: International Sales international@pearson.com Visit us on the Web: informit.com/aw Copyright © 2012 Pearson Education, Inc. All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. To obtain permission to use material from this work, please submit a written request to Pearson Education, Inc., Permissions Department, One Lake Street, Upper Saddle River, New Jersey 07458, or you may fax your request to (201) 236-3290. ISBN-13: 978-0-13-297919-1 ISBN-10: 0-13-297919-5
Page
4
Second release, September 2012
Page
5
Note from the Publisher The Effective C++ Digital Collection includes three bestselling C++ eBooks: • Effective C++: 55 Specific Ways to Improve Your Programs and Designs, Third Edition • More Effective C++: 35 New Ways to Improve Your Programs and Designs • Effective STL: 50 Specific Ways to Improve Your Use of the Standard Template Library By combining these seminal works into one eBook collection, you have easy access to the pragmatic, down-to-earth advice and proven wisdom that Scott Meyers is known for. This collection is essential reading for anyone working with C++ and STL. To simplify access, we’ve appended “A” to pages of Effective C++, “B” to pages of More Effective C++, and ”C” to pages of Effective STL. This enabled us to produce a single, comprehensive table of contents and dedicated indexes so that you can easily link to the topics you want and navigate between the books. We hope you find this collection useful! —The editorial and production teams at Addison-Wesley
Page
6
Table of Contents Effective C++: 55 Specific Ways to Improve Your Programs and Designs, Third Edition Introduction Chapter 1. Accustoming Yourself to C++ Item 1: View C++ as a federation of languages Item 2: Prefer consts, enums, and inlines to #defines Item 3: Use const whenever possible Item 4: Make sure that objects are initialized before they're used Chapter 2. Constructors, Destructors, and Assignment Operators Item 5: Know what functions C++ silently writes and calls Item 6: Explicitly disallow the use of compiler-generated functions you do not want Item 7: Declare destructors virtual in polymorphic base classes Item 8: Prevent exceptions from leaving destructors Item 9: Never call virtual functions during construction or destruction Item 10: Have assignment operators return a reference to *this Item 11: Handle assignment to self in operator= Item 12: Copy all parts of an object Chapter 3. Resource Management Item 13: Use objects to manage resources. Item 14: Think carefully about copying behavior in resource-managing classes. Item 15: Provide access to raw resources in resource-managing classes. Item 16: Use the same form in corresponding uses of new and delete. Item 17: Store newed objects in smart pointers in standalone statements. Chapter 4. Designs and Declarations
Page
7
Item 18: Make interfaces easy to use correctly and hard to use incorrectly Item 19: Treat class design as type design Item 20: Prefer pass-by-reference-to-const to pass-by-value Item 21: Don't try to return a reference when you must return an object Item 22: Declare data members private Item 23: Prefer non-member non-friend functions to member functions Item 24: Declare non-member functions when type conversions should apply to all parameters Item 25: Consider support for a non-throwing swap Chapter 5. Implementations Item 26: Postpone variable definitions as long as possible. Item 27: Minimize casting. Item 28: Avoid returning “handles” to object internals. Item 29: Strive for exception-safe code. Item 30: Understand the ins and outs of inlining. Item 31: Minimize compilation dependencies between files Chapter 6. Inheritance and Object-Oriented Design Item 32: Make sure public inheritance models “is-a.” Item 33: Avoid hiding inherited names Item 34: Differentiate between inheritance of interface and inheritance of implementation Item 35: Consider alternatives to virtual functions Item 36: Never redefine an inherited non-virtual function Item 37: Never redefine a function's inherited default parameter value Item 38: Model “has-a” or “is-implemented-in-terms-of” through composition Item 39: Use private inheritance judiciously Item 40: Use multiple inheritance judiciously Chapter 7. Templates and Generic Programming Item 41: Understand implicit interfaces and compile-time polymorphism
Page
8
Item 42: Understand the two meanings of typename Item 43: Know how to access names in templatized base classes Item 44: Factor parameter-independent code out of templates Item 45: Use member function templates to accept “all compatible types.” Item 46: Define non-member functions inside templates when type conversions are desired Item 47: Use traits classes for information about types Item 48: Be aware of template metaprogramming Chapter 8. Customizing new and delete Item 49: Understand the behavior of the new-handler Item 50: Understand when it makes sense to replace new and delete Item 51: Adhere to convention when writing new and delete Item 52: Write placement delete if you write placement new Chapter 9. Miscellany Item 53: Pay attention to compiler warnings Item 54: Familiarize yourself with the standard library, including TR1 Item 55: Familiarize yourself with Boost. Appendix A. Beyond Effective C++ Appendix B. Item Mappings Between Second and Third Editions Index More Effective C++ 35 New Ways to Improve Your Programs and Designs Introduction Basics Item 1: Distinguish between pointers and references. Item 2: Prefer C++-style casts. Item 3: Never treat arrays polymorphically. Item 4: Avoid gratuitous default constructors.
Page
9
Operators Item 5: Be wary of user-defined conversion functions. Item 6: Distinguish between prefix and postfix forms of increment and decrement operators. Item 7: Never overload &&, ||, or ,. Item 8: Understand the different meanings of new and delete. Exceptions Item 9: Use destructors to prevent resource leaks. Item 10: Prevent resource leaks in constructors. Item 11: Prevent exceptions from leaving destructors. Item 12: Understand how throwing an exception differs from passing a parameter or calling a virtual function. Item 13: Catch exceptions by reference. Item 14: Use exception specifications judiciously. Item 15: Understand the costs of exception handling. Efficiency Item 16: Remember the 80-20 rule. Item 17: Consider using lazy evaluation. Item 18: Amortize the cost of expected computations. Item 19: Understand the origin of temporary objects. Item 20: Facilitate the return value optimization. Item 21: Overload to avoid implicit type conversions. Item 22: Consider using op= instead of stand-alone op. Item 23: Consider alternative libraries. Item 24: Understand the costs of virtual functions, multiple inheritance, virtual base classes, and RTTI. Techniques Item 25: Virtualizing constructors and non-member functions. Item 26: Limiting the number of objects of a class. Item 27: Requiring or prohibiting heap-based objects.
Page
10
Item 28: Smart pointers. Item 29: Reference counting. Item 30: Proxy classes. Item 31: Making functions virtual with respect to more than one object. Miscellany Item 32: Program in the future tense. Item 33: Make non-leaf classes abstract. Item 34: Understand how to combine C++ and C in the same program. Item 35: Familiarize yourself with the language standard. Recommended Reading An auto_ptr Implementation General Index Index of Example Classes, Functions, and Templates Effective STL 50 Specific Ways to Improve Your Use of the Standard Template Library Introduction Chapter 1. Containers Item 1: Choose your containers with care Item 2: Beware the illusion of container-independent code Item 3: Make copying cheap and correct for objects in containers Item 4: Call empty instead of checking size() against zero Item 5: Prefer range member functions to their single-element counterparts Item 6: Be alert for C++’s most vexing parse Item 7: When using containers of newed pointers, remember to delete the pointers before the container is destroyed Item 8: Never create containers of auto_ptrs Item 9: Choose carefully among erasing options
Page
11
Item 10: Be aware of allocator conventions and restrictions Item 11: Understand the legitimate uses of custom allocators Item 12: Have realistic expectations about the thread safety of STL containers Chapter 2. vector and string Item 13: Prefer vector and string to dynamically allocated arrays Item 14: Use reserve to avoid unnecessary reallocations Item 15: Be aware of variations in string implementations Item 16: Know how to pass vector and string data to legacy APIs Item 17: Use “the swap trick” to trim excess capacity Item 18: Avoid using vector<bool> Chapter 3. Associative Containers Item 19: Understand the difference between equality and equivalence Item 20: Specify comparison types for associative containers of pointers Item 21: Always have comparison functions return false for equal values Item 22: Avoid in-place key modification in set and multiset Item 23: Consider replacing associative containers with sorted vectors Item 24: Choose carefully between map::operator[] and map::insert when efficiency is important Item 25: Familiarize yourself with the nonstandard hashed containers Chapter 4. Iterators Item 26: Prefer iterator to const_iterator, reverse_iterator, and const_reverse_iterator Item 27: Use distance and advance to convert a container’s const_iterators to iterators Item 28: Understand how to use a reverse_iterator’s base iterator Item 29: Consider istreambuf_iterators for character-by-character input Chapter 5. Algorithms Item 30: Make sure destination ranges are big enough Item 31: Know your sorting options
Page
12
Item 32: Follow remove-like algorithms by erase if you really want to remove something Item 33: Be wary of remove-like algorithms on containers of pointers Item 34: Note which algorithms expect sorted ranges Item 35: Implement simple case-insensitive string comparisons via mismatch or lexicographical_compare Item 36: Understand the proper implementation of copy_if Item 37: Use accumulate or for_each to summarize ranges Chapter 6. Functors, Functor Classes, Functions, etc Item 38: Design functor classes for pass-by-value Item 39: Make predicates pure functions Item 40: Make functor classes adaptable Item 41: Understand the reasons for ptr_fun, mem_fun, and mem_fun_ref Item 42: Make sure less<T> means operator< Chapter 7. Programming with the STL Item 43: Prefer algorithm calls to hand-written loops Item 44: Prefer member functions to algorithms with the same names Item 45: Distinguish among count, find, binary_search, lower_bound, upper_bound, and equal_range Item 46: Consider function objects instead of functions as algorithm parameters Item 47: Avoid producing write-only code Item 48: Always #include the proper headers Item 49: Learn to decipher STL-related compiler diagnostics Item 50: Familiarize yourself with STL-related web sites Bibliography Appendix A. Locales and Case-Insensitive String Comparisons Appendix B. Remarks on Microsoft’s STL Platforms Index
Page
13
Effective C++ 55 Specific Ways to Improve Your Programs and Designs, Third Edition Scott Meyers Upper Saddle River, NJ • Boston • Indianapolis • San Francisco New York • Toronto • Montreal • London • Munich • Paris • Madrid Capetown • Sydney • Tokyo • Singapore • Mexico City
Page
14
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals. The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests. For more information, please contact: U.S. Corporate and Government Sales (800) 382-3419 corpsales@pearsontechgroup.com For sales outside the U.S., please contact: International Sales international@pearson.com Visit us on the Web: www.awprofessional.com Library of Congress Control Number: 2005924388 Copyright © 2005 Pearson Education, Inc. All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, write to: Pearson Education, Inc. Rights and Contracts Department One Lake Street Upper Saddle River, NJ 07458 ISBN 0-321-33487-6
Page
15
Text printed in the United States on recycled paper at Courier in Westford, Massachusetts. First printing, May 2005
Page
16
For Nancy, without whom nothing would be much worth doing Wisdom and beauty form a very rare combination. —Petronius Arbiter Satyricon, XCIV And in memory of Persephone, 1995–2004
Page
17
Praise for Effective C++, Third Edition “Scott Meyers' book, Effective C++, Third Edition, is distilled programming experience — experience that you would otherwise have to learn the hard way. This book is a great resource that I recommend to everybody who writes C++ professionally.” — Peter Dulimov, ME, Engineer, Ranges and Assessing Unit, NAVSYSCOM, Australia “The third edition is still the best book on how to put all of the pieces of C++ together in an efficient, cohesive manner. If you claim to be a C++ programmer, you must read this book.” — Eric Nagler, Consultant, Instructor, and author of Learning C++ “The first edition of this book ranks among the small (very small) number of books that I credit with significantly elevating my skills as a 'professional' software devel-oper. Like the others, it was practical and easy to read, but loaded with important advice. Effective C++, Third Edition, continues that tradition. C++ is a very powerful programming language. If C gives you enough rope to hang yourself, C++ is a hard- ware store with lots of helpful people ready to tie knots for you. Mastering the points discussed in this book will definitely increase your ability to effectively use C++ and reduce your stress level.” — Jack W. Reeves, Chief Executive Officer, Bleading Edge Software Technologies “Every new developer joining my team has one assignment — to read this book.” — Michael Lanzetta, Senior Software Engineer “I read the first edition of Effective C++ about nine years ago, and it immediately became my favorite book on C++. In my opinion, Effective C++, Third Edition, remains a must-read today for anyone who wishes to program effectively in C++. We would live in a better world if C++
Page
18
programmers had to read this book before writing their first line of professional C++ code.” — Danny Rabbani, Software Development Engineer “I encountered the first edition of Scott Meyers' Effective C++ as a struggling programmer in the trenches, trying to get better at what I was doing. What a lifesaver! I found Meyers' advice was practical, useful, and effective, fulfilling the promise of the title 100 percent. The third edition brings the practical realities of using C++ in serious development projects right up to date, adding chapters on the language's very latest issues and features. I was delighted to still find myself learning something interesting and new from the latest edition of a book I already thought I knew well.” — Michael Topic, Technical Program Manager “From Scott Meyers, the guru of C++, this is the definitive guide for anyone who wants to use C++ safely and effectively, or is transitioning from any other OO language to C++. This book has valuable information presented in a clear, concise, entertaining, and insightful manner.” — Siddhartha Karan Singh, Software Developer “This should be the second book on C++ that any developer should read, after a general introductory text. It goes beyond the how and what of C++ to address the why and wherefore. It helped me go from knowing the syntax to understanding the philosophy of C++ programming.” — Timothy Knox, Software Developer “This is a fantastic update of a classic C++ text. Meyers covers a lot of new ground in this volume, and every serious C++ programmer should have a copy of this new edition.” — Jeffrey Somers, Game Programmer “Effective C++, Third Edition, covers the things you should be doing when writing code and does a terrific job of explaining why those things are important. Think of it as best practices for writing C++.”
Page
19
— Jeff Scherpelz, Software Development Engineer “As C++ embraces change, Scott Meyers' Effective C++, Third Edition, soars to remain in perfect lock-step with the language. There are many fine introductory books on C++, but exactly one second book stands head and shoulders above the rest, and you're holding it. With Scott guiding the way, prepare to do some soaring of your own!” — Leor Zolman, C++ Trainer and Pundit, BD Software “This book is a must-have for both C++ veterans and newbies. After you have finished reading it, it will not collect dust on your bookshelf — you will refer to it all the time.” — Sam Lee, Software Developer “Reading this book transforms ordinary C++ programmers into expert C++ programmers, step-by-step, using 55 easy-to-read items, each describing one technique or tip.” — Jeffrey D. Oldham, Ph.D., Software Engineer, Google “Scott Meyers' Effective C++ books have long been required reading for new and experienced C++ programmers alike. This new edition, incorporating almost a decade's worth of C++ language development, is his most content-packed book yet. He does not merely describe the problems inherent in the language, but instead he provides unambiguous and easy-to-follow advice on how to avoid the pitfalls and write 'effective C++.' I expect every C++ programmer to have read it.” — Philipp K. Janert, Ph.D., Software Development Manager “Each previous edition of Effective C++ has been the must-have book for developers who have used C++ for a few months or a few years, long enough to stumble into the traps latent in this rich language. In this third edition, Scott Meyers extensively refreshes his sound advice for the modern world of new language and library features and the programming styles that have evolved to use them. Scott's engaging writing style makes it easy to assimilate his guidelines on your way to becoming an effective C++ developer.”
Page
20
— David Smallberg, Instructor, DevelopMentor; Lecturer, Computer Science, UCLA “Effective C++ has been completely updated for twenty-first-century C++ practice and can continue to claim to be the first second book for all C++ practitioners.” — Matthew Wilson, Ph.D., author of Imperfect C++
The above is a preview of the first 20 pages. Register to read the complete e-book.
AI Reading Assistant
Whole-book reading guide from stratified index samples; jump to passages in the text
AI guide
【One-Line Pitch】
A single-volume omnibus of Scott Meyers's three classic C++ books, offering a large set of focused, item-by-item guidelines for writing clearer, safer, and more efficient C++. Best suited to working C++ programmers who already know the language and want to sharpen their judgment rather than learn syntax from scratch.
【Book Arc】
- **Opening (~0%–5%)**: The collection opens as a bundle of three previously published works—*Effective C++, More Effective C++, and Effective STL*—framing the whole as a large set of practical "ways to improve your programming" rather than a linear tutorial.
- **Early (~5%–40%)**: The first book, *Effective C++,* establishes the foundation: habits and design decisions that separate competent C++ from fragile C++ (resource management, class design, inheritance, and interface discipline, per the book's reputation).
- **Middle (~40%–70%)**: *More Effective C++* builds on that base with more advanced idioms and edge cases, pushing from "correct" toward "expert" usage.
- **Late (~70%–95%)**: *Effective STL* shifts focus to the standard library, turning containers, iterators, and algorithms into tools used with intent rather than guesswork.
- **Ending (~95%–100%)**: The collection closes as a reference set—items meant to be revisited as situations arise, not read once and shelved.
【Key Takeaways】
- **Advice is organized as discrete items, not chapters** (Early): the format lets you jump to a specific problem and get a concrete recommendation, making the collection a reference as much as a read-through.
- **The through-line is judgment, not trivia** (Early): the value lies in learning *why* one approach beats another, so the lessons transfer to code the books never mention.
- **Three books form a progression** (Middle): *Effective C++* covers core practice, *More Effective C++* goes deeper into advanced technique, and *Effective STL* specializes in library usage—read in that order for the intended ramp.
- **Resource and design discipline come first** (Early): the foundational material stresses ownership, copying, and class/inheritance decisions—the areas where C++ bugs are most expensive.
- **The STL section rewards deliberate use** (Late): containers and algorithms are presented as choices with trade-offs, not defaults to memorize.
- **It assumes prior fluency** (Early): the collection is written for programmers who already write C++; beginners will find the item format dense without that base.
- **Re-reading is part of the design** (Ending): many items only land once you've hit the problem they address, so the book grows more useful with experience.
【Reading Tips】
- Read *Effective C++* first and in order; treat *More Effective C++* and *Effective STL* as targeted references you dip into when a relevant problem appears.
- Keep a scratch file of items that contradict your current habits—those are the highest-value pages for you personally.
- Don't try to absorb all items at once; pick a handful per week and apply them to real code, since the lessons are behavioral.
- Skim items whose titles describe practices you already follow; deep-read the ones that make you defensive—that reaction usually marks a real gap.
- Use the collection as a checklist during code review rather than as a cover-to-cover read.
【Coverage Limits】
The supplied excerpts contain only the collection's title, author, and blurb—no item text, chapter titles, or examples. This guide therefore describes the collection's structure and reputation from that framing; specific items, code samples, and edition differences are not covered by the excerpts and are not asserted here.
Passage locations
Excerpt 1
书名: Effective C++ Digital Collection 140 Ways to Improve Your Programming (Scott Meyers) (z-library.sk, 1lib.sk, z-lib.sk) 作者: Scott Meyers Scott Meyers’s se...
View in text
Recommended for You
{{#thumbnailUrl}}
{{/thumbnailUrl}}
{{^thumbnailUrl}}
{{/thumbnailUrl}}
Loading recommended books...
Failed to load, please try again later
Tip the Site
Scan the WeChat Pay or Alipay code to tip. No login required.
WeChat Pay
Alipay