In chess, learning the rules for how the pieces move is simply the first step; to master the game, you must understand the strategies and tactics that guide each move. The same applies to C++. Knowing the right strategies helps you avoid the traps and work more effectively. Luminary Rob Murray helps beginning to intermediate C++ programmers take that next step by sharing his experience and advice.
Practical code examples are used throughout to illuminate useful programming strategies and warn against dangerous practices. To further ensure comprehension, each chapter ends with a list of the key ideas introduced in that chapter, along with questions to stimulate thought and discussion.
You'll begin by learning how to choose the right abstractions for your design, taking care to preserve the distinction between abstraction and implementation. You'll then look at the process of turning an abstraction into one or more C++ classes, with discussions ranging from high-level design strategies to low-level interface and implementation details.
Single and multiple inheritance are explored in depth, beginning with a discussion of where they fit in a design and followed by detailed examples that show how the concepts are used in practice. A separate chapter covers the techniques of building classes from which others can derive, and discusses the benefits - and costs - involved.
Rob Murray offers unprecedented insight into the new templates feature, beginning with the basics and walking you through several real-world examples. The author also describes a variety of specific techniques to make your programs faster, more reusable, and more robust. Exceptions are another new C++ feature; Murray gives advice on when they should - and should not - be used. The book concludes with a look at the process of moving a project from C to C++, exploring the human issues as well as the technical ones.
A Note about The Code Examples.
A Road Map.
The Language Rules.
A Telephone Number Abstraction.
Relationships Between Abstractions.
Worrying about the Boundary Conditions.
Designing with CRC Cards.
Implicit Type Conversions.
Overloaded Operators: Members or Nonmembers?
Overloading, Defaults, and Ellipsis.
Questions. CHAPTER = 3. Handles.
Avoiding Copies with Use Counts.
Avoiding Recompilations: The Cheshire Cat.
Using Handles to Hide the Design.
Handles as Objects.
The is-a Relationship.
Conformance to Base Class Abstractions.
Pure Virtual Functions.
Inheritance Details and Traps.
Multiple Inheritance as Set Intersection.
Virtual Base Classes.
Some Multiple Inheritance Details.
The Protected Interface.
Should You Design for Inheritance?
Design for Inheritance: Some Examples.
A Pair Class Template.
Some Template Details.
Expression Arguments to Templates.
Container Classes Using Templates.
Example: A Block Class.
Detailed Block Design Issues.
Containers with Iterators: A List Class.
Iterator Design Issues.
Constraints on Template Arguments.
Discovery and Acquisition.
Alternative Memory Allocation Schemes.
Passing Arguments to Operator New.
Managing External Resources.
Finding Memory Bugs.
Don't Guess, Measure!
Bottlenecks in Dynamic Memory Allocation.
An Example Exception.
Exceptions Should Be Exceptional.
Designing the Exception Object.
Design and Implementation.
Developing an Asset Base.
A Closing Thought.
In the hands of an expert, C++ helps designers and programmers build systems that are modular, maintainable, and fast. To the novice, however, the size of the language can be intimidating. There are a lot of features in C++ and it takes some experience to learn which ones are appropriate for any situation.
This book is intended to enhance and expedite that learning process. Most successful C++ programmers cannot recite chapter and verse from the language rules; instead, they have acquired a set of idioms and techniques that have worked well for them. Our goal is to help the C++ novice learn those idioms that have been most useful in practice. We also point out some of the most common pitfalls.
We do not try to cover the entire language and we leave the ultra-precise definitions of language semantics to the reference manuals. Instead, we concentrate on helping the reader build programs that can be understood by someone who is not a C++ language lawyer. We not only discuss techniques for making programs elegant and fast; we also show how to make them easier to understand and maintain.
Acknowledgements Almost none of the ideas and programming idioms in this book are my invention. My goal has been to present, in a way that allows novice C++ programmers to learn them quickly, what I consider to be the most important strategies and tactics I have learned from others in the eight years I have been using C++. Some of these lessons were learned by studying actual development projects as they moved from C to C++; others came from discussions with talented individuals.
Many of the best ideas on templates and library design, including the ideas behind many of the container classes in this book, came from classes in the USL Standard Components that were originally designed by Martin Carroll, Andrew Koenig, and Jonathan Shopiro. I claim exclusive ownership of any errors in my versions. Andrew Koenig was a valuable resource as the local C++ language lawyer. The participants in the "C++ Strategies and Tactics" seminars I presented at several conferences helped inspire this book and refine its ideas. Other important ideas came from Tom Cargill, John Carolan, Jim Coplien, Mark Linton, Gerald Schwarz, and of course Bjarne Stroustrup, who also invented the C++ programming language that made the book possible in the first place.
Brian Kernighan read several drafts of this book, and his excellent feedback has been a lot of help. I would also like to thank David Annatone, Steve Buroff, Tom Cargill, Bill Hopkins, Cay Horstman, Lorraine Juhl, Peter Juhl, Stan Lippman, Dennis Mancl, Scott Meyers, Barbara Moo, Lorraine Weisbrot Murray, Bjarne Stroustrup, Clovis Tondo, Steve Vinoski, and Christopher Van Wyk for their comments on early drafts of this book. Lorraine Weisbrot Murray also contributed the encouragement, understanding, support, and love that helped make the entire effort feasible.