Think In C++.doc

(3622 KB) Pobierz
Thinking in C++ 2nd ed Volume 2

Revision 10 (October 15, 2002) –

Chapters 1 through 3 are now 100% complete (copy-edited and tech-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 (IOStream). 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:

·         Added a wide-character version of ichar_traits

·         Replaced SiteMapConvert.cpp with ExtractCode.cpp

·         Added exercises

Revision 6 (July 27, 2002) --

Finished Chapter 3 (Strings)

·         Mentioned caveat about reference counting with multithreading.

·         Removed first (out-of-date) HTML example

·         Fixed the ichar_traits example

·         Fixed stupid MemCheck.cpp error in Chapter 2

Revision 5 (July 20, 2002) --

Chapters 1 and 2 are “finished”.

·         Reordered the material in Chapter 1:

o       Placed exception specifications last, and warned of their dangers with template classes

o       Added a section on Exception Safety.

o       Added material on auto_ptr

o       Added material illustrating bad_exception

o       Explained the internal implementation of exceptions and the Zero-cost Model

·         Merged Chapter 3 (Debugging) into Chapter 2:

o       Added material on invariants, assertions and Design-by-contract

o       Placed the TestSuite in its own namespace

o       Finished the MemCheck system for tracking memory errors

·         Removed Chapter 11 (Design Patterns)

o       Will be replaced by Chapter 10 (Concurrent Programming)

Revision 4, August 19, 2001 --

·         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.

·         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).

·         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.

·         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.

·         Added //{L} ../TestSuite/Test in all the programs 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
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



 

Thinking

In

C++

2nd Edition
Volume 2: Practical Programming

Bruce Eckel, President, MindView Inc.
Chuck Allison, Utah Valley State College






©2002 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 tirelessly worked
toward the development of the C++ language



 

What’s inside...


Preface              19

Goals              19

Chapters              20

Exercises              22

Exercise solutions              22

Source code              22

Language standards              24

Language support              24

Seminars, CD-ROMs & consulting              24

Errors              25

About the cover              25

Acknowledgements              25

Part 1: Building Stable Systems              29

1: Exception handling              31

Error handling in C              32

Throwing an exception              35

Catching an exception              36

The try block              36

Exception handlers              37

Exception matching              40

Catching any exception              42

Re-throwing an exception              43

Uncaught exceptions              44

Cleaning up              46

Resource management              48

Making everything an object              50

auto_ptr              53

Function-level try blocks              54

Standard exceptions              56

Exception specifications              60

Better exception specifications?              66

Exception specifications and inheritance              66

When not to use exception specifications              68

Exception safety              69

Programming with exceptions              74

When to avoid exceptions              74

Typical uses of exceptions              76

Overhead              80

Summary              83

Exercises              83

2: Defensive Programming              85

Assertions              88

The simplest automated unit test framework that could possibly work              93

Automated testing              95

The TestSuite Framework              99

Test suites              103

The test framework code              105

Debugging techniques              113

Trace macros              113

Trace file              114

Finding memory leaks              115

Summary              123

Exercises              123

Part 2: The Standard C++ Library              127

3: Strings in Depth              130

What’s in a string?              131

Creating and initializing C++ strings              133

Operating on strings              137

Appending, inserting, and concatenating strings              138

Replacing string characters              140

Concatenation using nonmember overloaded operators              145

Searching in strings              146

Finding in reverse              151

Finding first/last of a set of characters              153

Removing characters from strings              155

Comparing strings              157

Strings and character traits              162

A string application              169

Summary              175

Exercises              176

4: Iostreams              177

Why iostreams?              177

Iostreams to the rescue              183

Inserters and extractors              183

Common usage              188

Line-oriented input              191

Handling stream errors              193

File iostreams              196

A File-Processing Example              198

Open modes              200

Iostream buffering              201

Seeking in iostreams              204

String iostreams              208

Input string streams              209

Output string streams              211

Output stream formatting              215

Format flags              216

Format fields              218

Width, fill, and precision              220

An exhaustive example              221

Manipulators              225

Manipulators with arguments              226

Creating manipulators              230

Effectors              232

Iostream examples              234

Maintaining class library source code              234

Detecting compiler errors              239

A simple datalogger              242

Internationalization              248

Wide Streams              248

Locales              251

Summary              254

Exercises              255

5: Templates in depth              257

Nontype template arguments              257

Default template arguments              258...

Zgłoś jeśli naruszono regulamin