本书指导读者如何充分利用标准C和C++库,其涵盖容器、迭代器、算法、文本与文件处理、时间与日期处理以及内存管理等内容。同时本书提供了非常实用的有关抽象、模板、二进制处理、可见度、控制结构和异常处理的使用技巧。\r\n\r\n 本书还提供了C和C++的大量编程范例,对于进行实用开发的人员具有很大的借鉴和指导意义。各类读者均能从中获得裨益,提高C/C++的编程水平。\r\n
\r\n
Foreword \r\n\r\n Bruce Eckel \r\n\r\n Preface \r\n\r\n C++: The Making of a Standard \r\n\r\n An Interview with Bjarne Stroustrup \r\n\r\n Part I Preliminaries \r\n\r\n 1 A Better C \r\n\r\n A Tale Of Two Languages \r\n\r\n Incremental Journey \r\n\r\n The Type System \r\n\r\n Function Prototypes \r\n\r\n Type-safe Linkage \r\n\r\n References \r\n\r\n Type-safe I/O \r\n\r\n Standard Streams \r\n\r\n Formatting \r\n\r\n Manipulators \r\n\r\n Function Overloading and Function Templates \r\n\r\n Operator Overloading \r\n\r\n Inline Functions \r\n\r\n Default Arguments \r\n\r\n new and delete \r\n\r\n Statement Declarations \r\n\r\n Standard Library Features \r\n\r\n C Compatibility \r\n\r\n Summary \r\n\r\n 2 Pointers \r\n\r\n Programming on the Edge \r\n\r\n The Basics \r\n\r\n Pointer Arithmetic \r\n\r\n Pass-By-Reference Semantics \r\n\r\n Generic Pointers \r\n\r\n const Pointers \r\n\r\n Pointers and One-Dimensional Arrays \r\n\r\n Arrays as Parameters \r\n\r\n Arrays of Strings \r\n\r\n Pointers and Multidimensional Arrays \r\n\r\n Higher and Deeper \r\n\r\n Pointers to Functions \r\n\r\n Pointers to Member Functions \r\n\r\n Encapsulation and Incomplete Types \r\n\r\n Summary \r\n\r\n 3 The Preprocessor \r\n\r\n The #include Directive \r\n\r\n Other Preprocessor Directives \r\n\r\n Predefined Macros \r\n\r\n Conditional Compilation \r\n\r\n Preprocessing Operators \r\n\r\n Implementing assert \r\n\r\n Macro Magic \r\n\r\n Character Sets, Trigraphs, and Digraphs \r\n\r\n Phases of Translation \r\n\r\n Summary \r\n\r\n 4 The Standard C Library, Part I \r\n\r\n For the Adequate Programmer \r\n\r\n \r\n\r\n \r\n\r\n \r\n\r\n \r\n\r\n 5 The Standard C Library, Part II \r\n\r\n For the Polished Programmer \r\n\r\n \r\n\r\n \r\n\r\n \r\n\r\n \r\n\r\n Appendix 5.1: Character Sets \r\n\r\n Code Pages \r\n\r\n Character Set Standards \r\n\r\n ISO 10646 \r\n\r\n Unicode \r\n\r\n 6 The Standard C Library, Part III \r\n\r\n For the Complete Programmer \r\n\r\n \r\n\r\n \r\n\r\n \r\n\r\n \r\n\r\n \r\n\r\n \r\n\r\n \r\n\r\n va_list's As Arguments \r\n\r\n An Application \r\n\r\n Conclusion \r\n\r\n Appendix 6.1: Floating-point Number Systems \r\n\r\n Part II Key Concepts \r\n\r\n 7 Abstraction \r\n\r\n Data Abstraction \r\n\r\n Operator Overloading \r\n\r\n Concrete Data Types \r\n\r\n Type Abstraction \r\n\r\n Function Abstraction \r\n\r\n Summary \r\n\r\n 8 Templates \r\n\r\n Generic Programming \r\n\r\n Function Templates \r\n\r\n Class Templates \r\n\r\n Template Parameters \r\n\r\n Template Specialization \r\n\r\n Summary \r\n\r\n 9 Bit Manipulation \r\n\r\n Bitwise Operators \r\n\r\n Accessing Individual Bits \r\n\r\n Large Bitsets \r\n\r\n Bit Strings \r\n\r\n Wish List \r\n\r\n The bitset Template \r\n\r\n The vector Template Specialization \r\n\r\n Summary \r\n\r\n 10 Conversions and Casts \r\n\r\n Integral Promotion \r\n\r\n Demotions \r\n\r\n Arithmetic Conversions \r\n\r\n Function Prototypes \r\n\r\n Explicit Conversions \r\n\r\n Function-style Casts \r\n\r\n Const Correctness \r\n\r\n User-Defined Conversions \r\n\r\n Beefing up operator [ ] \r\n\r\n New-Style Casts \r\n\r\n Summary \r\n\r\n 11 Visibility \r\n\r\n What's In a Name? \r\n\r\n Scope \r\n\r\n Minimal Scoping \r\n\r\n Class Scope \r\n\r\n Nested Classes \r\n\r\n Local Classes \r\n\r\n Classic Namespaces \r\n\r\n Namespace Scope \r\n\r\n Lifetime \r\n\r\n Lifetime of Temporaries \r\n\r\n Linkage \r\n\r\n Type-safe Linkage \r\n\r\n 'Language' Linkage \r\n\r\n Summary \r\n\r\n 12 Control Structures \r\n\r\n Structured Programming \r\n\r\n Branching \r\n\r\n Nonlocal Branching \r\n\r\n Signals \r\n\r\n Summary \r\n\r\n References \r\n\r\n 13 Exceptions \r\n\r\n Error Handling Alternatives \r\n\r\n Stack Unwinding \r\n\r\n Catching Exceptions \r\n\r\n Standard Exceptions \r\n\r\n Resource Management \r\n\r\n Constructors and Exceptions \r\n\r\n Memory Management \r\n\r\n Exception Specifications \r\n\r\n An Error-Handling Strategy \r\n\r\n Summary \r\n\r\n 14 Object-oriented Programming \r\n\r\n Inheritance \r\n\r\n Heterogeneous Collections \r\n\r\n Virtual Functions and Polymorphism \r\n\r\n Abstract Base Classes \r\n\r\n Case Study: A Framework for Object Persistence \r\n\r\n Database Access \r\n\r\n Mapping Objects to Relational Schema \r\n\r\n PFX Architecture \r\n\r\n A Code Walkthrough \r\n\r\n Summary \r\n\r\n Part III Leveraging the Standard Library \r\n\r\n 15 Algorithms \r\n\r\n Complexity \r\n\r\n Generic Algorithms \r\n\r\n Function Objects \r\n\r\n Function Taxonomy \r\n\r\n Function Object Adapters \r\n\r\n Algorithm Taxonomy \r\n\r\n Summary \r\n\r\n References \r\n\r\n 16 Containers and Iterators \r\n\r\n Standard Containers \r\n\r\n Iterators \r\n\r\n Iterator Taxonomy \r\n\r\n Special-Purpose Iterators \r\n\r\n Container Adapters \r\n\r\n Associative Containers \r\n\r\n Applications \r\n\r\n Non-STL Containers \r\n\r\n Summary \r\n\r\n 17 Text Processing \r\n\r\n scanf \r\n\r\n printf \r\n\r\n Substrings \r\n\r\n The Standard C++ String Class \r\n\r\n String Streams \r\n\r\n Wide Strings \r\n\r\n Summary \r\n\r\n 18 File Processing \r\n\r\n Filters \r\n\r\n Binary Files \r\n\r\n Record Processing \r\n\r\n Temporary Files \r\n\r\n Portability \r\n\r\n POSIX \r\n\r\n File Descriptors \r\n\r\n Copying Files via Descriptors \r\n\r\n Reading Directory Entries \r\n\r\n Redirecting Standard Error \r\n\r\n Encapsulating Directory Operations \r\n\r\n Summary \r\n\r\n 19 Time and Date Processing \r\n\r\n Julian Day Encoding \r\n\r\n Date Classes for Real Work \r\n\r\n Calculating the Week of the Year \r\n\r\n Summary \r\n\r\n Reference \r\n\r\n 20 Dynamic Memory Management \r\n\r\n Ragged Arrays \r\n\r\n Using the Heap in Standard C \r\n\r\n The C++ Free Store \r\n\r\n Deep versus Shallow Copy \r\n\r\n Handling Memory Allocation Failure \r\n\r\n Overriding new and delete \r\n\r\n Placement new \r\n\r\n Managing the Heap \r\n\r\n Avoiding Memory Management \r\n\r\n Summary \r\n\r\n Appendices \r\n\r\n A C/C++ Compatibility \r\n\r\n B Standard C++ Algorithms \r\n\r\n C Function Objects and Adapters \r\n\r\n Function Objects \r\n\r\n Function Object Adapters \r\n\r\n D Annotated Bibliography \r\n\r\n The C Practitioner's Booklist \r\n\r\n The C++ Practitioner's Booklist \r\n\r\n Index \r\n
\r\n
I first heard about Chuck when his bit handling classes, bitset and bitstring, were accepted into the standard C++ library in March 1993(bitstring was later absorbed when the STL was added). While I myself did add a couple of tiny bits to the C++ standard here and there, the idea of successfully running an entire class through the terrible gauntlet of the ANSI/ISO C++ committee (on which we were both participants, so we knew) impressed me greatly.
But in the world of computers, which is so full of overstatement that I must ironically rely on my intuition rather than my intellect to discern truth, what impresses me more is someone who can explain things in a simple, clear, and non-overblown way. That is, a great teacher. Chuck is a great teacher. You can see it in the activities he finds himself compelled to do: writing, teaching, editing, explaining. When I find someone like this--and I know for sure when I see them speak to an audience--I convince them to speak at the Software Development (SD) conference (where I chair the C++ and Java tracks). Chuck has become a regular fixture at the conference, satisfying audiences on both coasts.
At the last SD conference (Fall 1997 in Washington, DC), it was Chuck's birthday, and when we found this out a group of us took him out to dinner. It was only after we were finally seated that I looked around and realized we were all authors: Bjarne Stroustrup (creator of C++ and author of The C++ Programming Language), Dan Saks (C++ columnist, speaker, consultant, and long-time secretary of the ANSI/ISO C++ committee), Bobby Schmidt (CUJ columnist, speaker), Marco Cantu (author of the "Mastering Delphi" books as well as C++ books), Tim Gooch (editor of the Cobb Group publications on C++ and now Java), and myself. These are the folks who respect Chuck enough to buy him dinner.
There are, of course, lots of"introduction to C++" books. Sometimes I feel like I keep trying to write a book on that subject over and over (my--I hope--final effort was Thinking in C++). But what happens when you've understood the basics and you want more depth? Books exist, but they can often be written in the tongue of the experts (a language that leaves me gasping) or they cover topics that are too esoteric or advanced. This book provides a bridge to the world of advanced topics; it gives you what, you need but it won't overwhelm you in the process.
Chuck has made his book both clear and accurate, and accuracy is something else I'm exceptionally fond of. When a book has too many flaws I grow tired of it (in the early days we had to put up with such things, but now there are enough carefully created C++ books that there's no reason to waste your time). Another thing I like a lot about this book is the brevity of the chapters, and the way each one is focused on a single topic, so I can pick it up and get an entire concept at once (I have a somewhat short attention span). This is a book that you will enjoy over time as it hands you one insight after another.
Bruce Eckel
http://www.EckelObjects.com
October 1997
This book is for people who program in C and C++ for a living. It assumes that you are already familiar with the syntax and basic constructs of both languages, and it offers practical wisdom for creating effective, real-world programs. Each code capsule, or sample program, contains working code illustrating proven idioms and techniques that leverage the power of these important programming languages.
This book serves as a voice of experience for those who wish to strengthen their skills and improve their effectiveness in the workplace. Despite current fervor for the object-oriented paradigm (which this book abundantly embraces), I make no excuse for paying homage to the C foundations of C++. I have found too many developers ill-prepared to master C++ because they lack a thorough understanding of basic concepts such as pointers, scope, linkage, and static type checking. Perhaps the biggest deficiency of all is a lack of familiarity with the standard C library. It is sad indeed when developers waste time reinventing what the library functions already provide so well. The C++ novice is often too eager to abandon (i.e., gloss over) simple C in favor of the "exciting" features of C++, such as inheritance, exceptions, or overriding operator new, even when such are not warranted. 1 feel confident that everyone will learn something from these pages. Chapters 1 and 13 through 16 are strictly C++ chapters, and Chapters 4 through 6 apply only to the C language. All other chapters cover both the C and C++ aspects of their respective topic.
That said, this is primarily a C++ book. As it goes to press, the C++ standardization effort is in its home stretch. The second public committee draft (CD2) has completed its cycle and only minor edits remain. As a member of this committee since early 1991, I have seen its document grow from 200 to over 750 pages. We have added exceptions, templates, namespaces, runtime type identification (RTTI) and other features to the language, and a sophisticated, templatized system of interrelated algorithms, containers, and iteration constructs to the library (commonly known as the Standard Template Library, or STL). Unlike other standards efforts, this committee has concentrated as much on invention as on standardizing existing practice. The overwhelming intricacies of C++ caused one Internet surfer to post this message: "If C gives you enough rope to hang yourself, then C++ gives you enough rope to hang everyone in your neighborhood, hoist the riggings of a small sailing ship, and still have enough left over to hang yourself." I have labored to illustrate and motivate standard C++ and its library in such a way that you might use your rope more wisely.
The first chapter (Chapter 0), an excerpt from an interview I conducted with Bjarue Stroustrup, records his feelings about the state of C++ as it becomes a standard. The rest of the book is divided into three parts.
Part I: Preliminaries
After a brief tour of C++, these chapters close some of the gaps a typical C programmer might have before s/he prepares to tackle C++. Chapter 2, "Pointers," is based on a well-received three-part series I ran in the C Users Journal in 1993. Chapters 4 through 6 cover what every professional should know about the standard C library, which is a crucial part of standard C++.
Part II: Key Concepts
This section thoroughly motivates and illustrates the concepts and features of the C++ language. Chapter 7 introduces data abstraction through classes, and Chapter 8 covers type abstraction as implemented by the C++ template mechanism. Templates are every bit as crucial to the effective use of C++ as objects are, perhaps even more so. Chapter 14 not only treats inheritance and polymorphism, but also illustrates object-oriented design and reuse as it presents a framework for object persistence that works with today's relational database management systems. The chapters in between give the reader depth in important fundamental concepts that too many developers tend to overlook.
Part III: Leveraging the Standard Library
Chapters 15 through 20 show howto use and appreciate the notable components of the standard C++ library, as well as elucidate some of the more sophisticated features of the standard C library that went beyond the scope of Chapters 4, 5, and 6. Chapters 15 and 16 explain why the STL subset of the library is what it is, and how to use it effectively. Chapter 19 contains a useful date component that can even handle partial dates, a common business data processing requirement.
In summary, this is book about what works. I've attempted to steer the reader away from the "gotchas" by illustrating "best practices" with a reasonable balance of breadth and depth. Why another C++ book in 19987 Because the language and library haven't stabilized until now. This book goes to press just one week after the standards committee met to approve the final draft of ISO C++, and I have taken care to steer clear of any dark corners that remain (all languages and environment have them). I am confident that all the material in this book will be timely for years to come.
Acknowledgments
This book has actually been in the making for more years than I care to admit. It began in 1984 when I chaired the Computer Science department at Pima Community College in Tucson, Arizona, and my colleague Claire Hamlet persuaded me to pursue a grant to develop the first course in C programming there. Thereafter I started collecting C program examples and shared them with my fellow employees at Hughes Aircraft Company and at the World Headquarters of the Church of Jesus Christ of Latter-day Saints in Salt Lake City. The "code capsule" idea, short vignettes with examples on particular topics, grew from my effort to make learning C fun and relatively painless for COBOL refugees. I was fortunate enough for a time to have management support in chairing an internal C Language Support Committee, a first-rate team of experienced programmers (David Coombs, John Pearson, Lorin Lords, Kent Olsen, Bill Owens, Drew Terry, and Mike Terrazas) that developed an outstanding curriculum and effectively trained over 100 Church employees.
The name "code capsules" occurred to me over breakfast with Lee Copeland in the Church cafeteria (he has a way of keeping me on my toes). Mike Terrazas reviewed early versions of these vignettes, and suggested that I present them to the C Users Journal, which I unwittingly did by showing them to P. J. "Bill" Plauger in London at the March 1992 meeting of the C++ Standards Committee. As senior editor, he proposed that I become a columnist for the Journal. The Code Capsule column ran from October 1992 until May 1995, when time commitments forced me to resign. A valued mentor and friend, Bill has also been instrumental in encouraging me to put these capsules in book form. Bruce Eckel was very kind to review portions of the book, and Pete Becker scoured the entire manuscript, uncovering a number of errors and inconsistencies. When it comes to "support," however, that necessary intangible that keeps one going, I must follow the example of almost every author who has ever written by thanking my family. Only Sandy, James, and Kim have a feel for the magnitude of the effort that has brought these pages to press. As 21-year-old James recently wrote from England, where he is spending two years, "So, Dad is finally finishing his book! He's been working on that for as long as I can remember."
Chuck Allison
http://www.freshsources.com
Novemver 1997