Effective C++

50 Specific Ways to Improve Your Programs and Designs
by Scott Meyers
ISBN 0-201-56364-9    224 pages    1992

Back cover copy

"This is a 193-page masterpiece that I read in one sitting... I guarantee that some combination of these 50 items will grab and enlighten you, and repay your modest investment... This is a well-written, honest book aimed at C++ programmers who are converging toward fluency and effectiveness." - Stan Kelly-Bootle, Unix Review

Scott Meyers presents 50 concise rules based on what experienced C++ developers almost always do, or almost always avoid, to create efficient, portable, and maintainable software. Each rule is accompanied by examples that illustrate the rule at work. If you want to be a proficient C++ programmer, you will need to understand the complex interactions between the many features in the language. This incisive guide offers insight into the most important interactions.

Among its unique benefits, this book offers:

This book corresponds to the definition of C++, including templates and exceptions, found in The Annotated C++ Reference Manual by Ellis and Stroustrup, and is also must reading for programmers with compilers supporting earlier versions of the language. With this book, you will learn how to write large scale software even if your compiler does not provide for nested types, templates, or exceptions. Meyers assumes a working knowledge of C++.

Scott Meyers, a recognized authority on C++, received his Ph.D. in computer science from Brown University. He is a columnist for the C++ Report and is also a senior member of the research and development staff at the Institute for Advanced Professional Studies, where he was the primary developer of their introductory and advanced C++ training courses.


Preface

This book is a direct outgrowth of my experiences teaching C++ to professional programmers through the Institute for Advanced Professional Studies. I found that most students, after a week of intensive instruction, felt comfortable with the basic constructs of the language, but were less sanguine about their ability to put the constructs together in an effective manner. Thus began my attempt to formulate short, specific, easy-to-remember guidelines for effective software development in C++: a summary of the things that experienced C++ programmers either almost always do or almost always avoid.

As a computer scientist, I was originally interested in rules that could be checked by a machine. To that end, I outlined a program to examine C++ software for constructs that were "almost always wrong." Currently under development, this checking program has become known as lint++. However, it quickly became apparent that the great majority of the guidelines used by good C++ programmers were too difficult to formalize, or had too many important exceptions, to be blindly enforced by a lint++-like program.

That led me to the notion of something less precise than a computer program but still more focused and to-the-point than a general C++ textbook. The result you now hold in your hands: a book containing 50 specific suggestions on how to improve your C++ programs and designs.

In this book you'll find advice on what you should do, and why, and what you should not do, and why not. Fundamentally, of course, the whys are much more important than the whats, but from a purely pragmatic point of view, it is much more convenient to have a list of guidelines in front of you than it is to memorize a textbook or two. Unlike most books on C++, my presentation here is not organized around particular language features. That is, I don't talk about constructors in one place, about virtual functions in another, about inheritance in a third, etc. Instead, each explanation is tightly coupled to the specific guideline it accompanies, and my coverage of the various aspects of a particular feature is typically dispersed throughout the book.

The advantage of this approach is that it better reflects the complexity of the software systems for which C++ is often chosen, systems in which the understanding of individual language features is not enough. For example, experienced C++ developers know that understanding inline functions and understanding virtual destructors does not necessarily mean that you understand inline virtual destructors. Such battle-scarred developers recognize that comprehending the interactions between the features in C++ is of the greatest possible importance in using the language effectively. The organization of this book reflects that fundamental truth.

The disadvantage of my approach is that you may have to look in more than one place to discover everything I have to say about a particular construct in C++. To minimize the inconvenience inherent in this approach, I have sprinkled cross-references liberally throughout the text, and a comprehensive index is provided at the end of the book.

The set of guidelines in this book is far from exhaustive, but coming up with good rules - ones that are applicable to almost all applications almost all the time - is harder than it looks. Perhaps you know of additional guidelines, of more ways in which to program effectively in C++. If so, I would be delighted to hear about them.

On the other hand, you may feel that some of the items in this book are inappropriate as general advice; that there is a better way to accomplish a task examined in the book; or that one or more of the technical discussions is unclear, incomplete, or misleading. I encourage you to let me know about these things, too.

Donald Knuth has a long history of offering a small reward for people who notify him of errors in his books. The quest for a perfect book is laudable in any case, but in view of the number of bug-ridden C++ books that have been rushed to market, I feel especially strongly compelled to follow Knuth's example. Therefore, for each error in this book that is reported to me - be it technical, grammatical, typographical, or otherwise - I will, in future printings, gladly acknowledge the first person to report that error.

Send your suggested guidelines, your comments, your criticisms, and - sigh - your bug reports to:

Scott Meyers
c/o Editor-in-Chief, Corporate and Professional Publishing
Addison-Wesley Publishing Company
1 Jacob Way
Reading, MA 01867
U. S. A.

Alternatively, you may send electronic mail to [email protected].

I maintain a list of changes to this book since its first printing, including bug-fixes, clarifications, and technical updates. This list is available via anonymous FTP from the Internet site wilma.cs.brown.edu in the file pub/Effective_C++_errata.txt. If you would like a copy of this file, but you lack access to the Internet, please send a request to one of the addresses above, and I will see that the list is sent to you.

SCOTT DOUGLAS MEYERS    PROVIDENCE, RHODE ISLAND
NOVEMBER 1991

Table of Contents


[email protected]
Last modified: Sun Apr 18 19:03:39 EETDST 1999