Thinking in C++ Volume 2 Annotated Solution Guide, Available for a Small Fee From
Total Page:16
File Type:pdf, Size:1020Kb
1 z 516 Note: This document requires the installation of the fonts Georgia, Verdana and Andale Mono (code font) for proper viewing. These can be found at: http://sourceforge.net/project/showfiles.php?group_id=34153&release_id=105355 Revision 19—(August 23, 2003) Finished Chapter 11, which is now going through review and copyediting. Modified a number of examples throughout the book so that they will compile with Linux g++ (basically fixing case- sensitive naming issues). Revision 18—(August 2, 2003) Chapter 5 is complete. Chapter 11 is updated and is near completion. Updated the front matter and index entries. Home stretch now. Revision 17—(July 8, 2003) Chapters 5 and 11 are 90% done! Revision 16—(June 25, 2003) Chapter 5 text is almost complete, but enough is added to justify a separate posting. The example programs for Chapter 11 are also fairly complete. Added a matrix multiplication example to the valarray material in chapter 7. Chapter 7 has been tech-edited. Many corrections due to comments from users have been integrated into the text (thanks!). Revision 15—(March 1 ,2003) Fixed an omission in C10:CuriousSingleton.cpp. Chapters 9 and 10 have been tech-edited. Revision 14—(January ,2003) Fixed a number of fuzzy explanations in response to reader feedback (thanks!). Chapter 9 has been copy-edited. Revision 13—(December 31, 2002) Updated the exercises for Chapter 7. Finished rewriting Chapter 9. Added a template variation of Singleton to chapter 10. Updated the build directives. Fixed lots of stuff. Chapters 5 and 11 still await rewrite. Revision 12—(December 23, 2002) Added material on Design Patterns as Chapter 10 (Concurrency will move to Chapter 11). Added exercises for Chapter 6. Here is the status of all chapters: 100% complete: 1-4, 6, 8 Copy-edited, waiting for tech edit: 7, 10 Incomplete: 5, 9, 11 Revision 11 (December 13, 2002) – Chapter 7 has been updated. Chapter 6 has been copy-edited and a few bugs were fixed. Chapter 4 2 z 516 has been tech-edited. The exercises are still out of date except for chapters 1-3. Revision 10 (October 15, 2002) – Chapters 1 through 3 are now 100% complete (copy-edited and tech-edited). Chapter 4 has been copy-edited. Updated Chapter 6 to fit in its new position and adding introductory material. (Chapters 5 and 7-10 are still unfinished at this point). Revision 9 (August 29, 2002) – Finished Chapter 4 (IOStreams). Reordered the material and added material on wide stream and locales. Removed references to strstreams. Edited the “Iostreams examples” section. Added new exercises. Revision 8 (August 6, 2002) -- Made ExtractCode.cpp in Chapter 3 work for GNU C++. Copy-edited Chapters 1 through 3. Revision 7 (July 31, 2002) -- Fixed omissions in comments for code extraction throughout text. Edited Chapter 3: z Added a wide-character version of ichar_traits z Replaced SiteMapConvert.cpp with ExtractCode.cpp z Added exercises Revision 6 (July 27, 2002) -- Finished Chapter 3 (Strings) z Mentioned caveat about reference counting with multithreading. z Removed first (out-of-date) HTML example z Fixed the ichar_traits example z Fixed stupid MemCheck.cpp error in Chapter 2 Revision 5 (July 20, 2002) -- Chapters 1 and 2 are “finished”. z Reordered the material in Chapter 1: { Placed exception specifications last, and warned of their dangers with template classes { Added a section on Exception Safety. { Added material on auto_ptr { Added material illustrating bad_exception 3 z 516 g p { Explained the internal implementation of exceptions and the Zero-cost Model z Merged Chapter 3 (Debugging) into Chapter 2: { Added material on invariants, assertions and Design-by-contract { Placed the TestSuite in its own namespace { Finished the MemCheck system for tracking memory errors z Removed Chapter 11 (Design Patterns) { Will be replaced by Chapter 10 (Concurrent Programming) Revision 4, August 19, 2001 -- z Restructured the book; this is the first version with Chuck Allison coauthoring. Incorporated Chuck's testing framework, which will be used throughout the book and automatically included as part of the book's build process in the makefiles. z In the code subdirectory of the unpacked distribution, you can now use make to compile for Borland, Microsoft, Gnu g++2.95 (distributed with Cygwin) and Gnu g++3.0 (tested under Linux). z Under Windows98/NT/2000, you will get best results running under the free Cygwin environment (www.Cygwin.com), even if you're compiling for Borland or Microsoft. In particular, some linker command lines for Microsoft are too long for Win98 COMMAND.COM, but work just fine under Cygwin. z Made many code changes to allow programs to be run inside the test framework, in particular removing the need for user input when executing programs. z Added //{L} ../TestSuite/Test in all the programs where the unit test framework is used that can be run without user input, to cause the makefile builder to generate calls to the programs as part of the build process. “This book is a tremendous achievement. You owe it to yourself to have a copy on your shelf. The chapter on iostreams is the most comprehensive and understandable treatment of that subject I’ve seen to date.” Al Stevens Contributing Editor, Doctor Dobbs Journal “Eckel’s book is the only one to so clearly explain how to rethink program construction for object orientation. That the book is also an excellent tutorial on the ins and outs of C++ is an added bonus.” Andrew Binstock Editor, Unix Review “Bruce continues to amaze me with his insight into C++, and Thinking in C++ is his best collection of ideas yet. If you want clear answers to difficult questions about C++, buy this outstanding book.” Gary Entsminger 4 z 516 yg Author, The Tao of Objects “Thinking in C++ patiently and methodically explores the issues of when and how to use inlines, references, operator overloading, inheritance and dynamic objects, as well as advanced topics such as the proper use of templates, exceptions and multiple inheritance. The entire effort is woven in a fabric that includes Eckel’s own philosophy of object and program design. A must for every C++ developer’s bookshelf, Thinking in C++ is the one C++ book you must have if you’re doing serious development with C++.” Richard Hale Shaw Contributing Editor, PC Magazine ThinkingIn Volume 2: C++Practical Programming Bruce Eckel, President, MindView, Inc. Chuck Allison, Utah Valley State College ©2004 MindView, Inc. The information in this book is distributed on an “as is” basis, without warranty. While every precaution has been taken in the preparation of this book, neither the author nor the publisher shall have any liability to any person or entitle with respect to any liability, loss or damage caused or alleged to be caused directly or indirectly by instructions contained in this book or by the computer software or hardware products described herein. All rights reserved. No part of this book may be reproduced in any form or by any electronic or mechanical means including information storage and retrieval systems without permission in writing from the publisher or authors, except by a reviewer who may quote brief passages in a review. Any of the names used in the examples and text of this book are fictional; any relationship to persons living or dead or to fictional characters in other works is purely coincidental. Dedication To all those who have worked tirelessly on the development of the C++ language 5 z 516 What’s inside... Preface 19 Goals........................................ 19 Chapters................................... 20 Exercises................................... 23 Exercise solutions............. 23 Source code............................... 23 Language standards................... 25 Language support............. 25 Seminars, CD-ROMs & consulting. 25 Errors....................................... 26 About the cover......................... 26 Acknowledgements..................... 26 Building Stable Systems 29 1: Exception handling 31 Traditional error handling............. 32 Throwing an exception................ 34 Catching an exception................. 36 The try block.................... 36 Exception handlers........... 36 Exception matching..................... 39 Catching any exception..... 42 Re-throwing an exception.. 42 Uncaught exceptions......... 43 Cleaning up................................ 45 Resource management..... 47 Making everything an object 49 auto_ptr.......................... 52 Function-level try blocks.... 53 Standard exceptions................... 55 Exception specifications............... 58 Better exception specifications? 64 Exception specifications and inheritance 64 When not to use exception specifications 66 Exception safety......................... 66 Programming with exceptions....... 71 When to avoid exceptions.. 71 Typical uses of exceptions. 73 Overhead................................... 77 Summary................................... 79 Exercises................................... 80 2: Defensive programming 83 Assertions................................. 86 A simple unit test framework........ 90 6 z 516 Automated testing............ 92 The TestSuite Framework.. 97 Test suites..................... 101 The test framework code. 102 Debugging techniques............... 110 Trace macros................. 110 Trace file....................... 111 Finding memory leaks..... 112 Summary................................. 119 Exercises................................. 120 The Standard C++ Library 125 3: Strings in depth 127 What’s in a string?...................