1. Trang chủ
  2. » Công Nghệ Thông Tin

Bruce eckel thinking in c++

1,1K 1,7K 1

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 1.132
Dung lượng 4,08 MB

Nội dung

Đây là quyển sách tiếng anh về lĩnh vực công nghệ thông tin cho sinh viên và những ai có đam mê. Quyển sách này trình về lý thuyết ,phương pháp lập trình cho ngôn ngữ C và C++.

nd Thinking in C++ edition VERSION TICA18 Revision history: TICA18: July 29, 1999 Rewrote chapter and added exercises Replaced all later instances of the “enum hack” with static const (which breaks VC++ a lot, since they still haven’t implemented this relatively ancient and simple feature) Added compiler support for Visual C++ 6.0 (+Service Pack 3) (although this hasn’t been tested with Microsoft’s nmake; you may need to edit the makefiles, copy nmake.exe to make.exe, or locate a gnu make in order to get it to work) You can see the results in CompileDB.txt in Appendix D Retested all code with Borland C++ Builder plus the downloadable update, and with egcs from July 18, 1999 Cleaned up some code files that were not being automatically compiled because they didn’t have main( )s TICA17: June 27, 1999 Rewrote chapter and added exercises Rewrote chapter and added exercises TICA16: June 1, 1999 Rewrote chapter and added exercises Modifications to chapter 19 before and after presentations at the SD conference Added “Factories” section to design patterns chapter Rechecked book code under May 24 build of egcs compiler TICA15: April 22, 1999 Rewrote chapter and added exercises TICA14, March 28, 1999 Rewrote Chapter and I think they’re both finished Chapter is rather big since it covers C syntax fundamentals, along with some C++ basics Added many exercises to Chapters & 3, to complete them both Chapter was a “hump” chapter; I think the others in section one shouldn’t be as hard Tried to conform all code in the book to the convention of “type names start with uppercase letters, functions and variables start with lowercase letters” TICA13, March 9, 1999 Thorough rewrite of chapter one, including the addition of UML diagrams I think chapter one is finished, now Reorganized material elsewhere in the book, but that is still in transit My goal right now is to move through all the chapters in section one, in order TICA12, January 15, 1999 Lots of work done on the Design Patterns chapter All the exsting programs are now modified and redesigned (significantly!) to compile under C++ Added several new examples Much of the prose in this chapter still needs work, and more patterns and examples are forthcoming Changed ExtractCode.cpp so that it generates “bugs” targets for each makefile, containing all the files that won’t compile with a particular compiler so they can be re-checked with new compilers Generates a master in the book’s root directory called makefile.bugs which descends into each subdirectory and executes make with “bugs” as a target and the –i flag so you’ll see all the errors TICA11, January 7, 1999 Completed the STL Algorithms chapter (significant additions and changes), edited and added examples the STL containers chapter Added many exercises at the ends of both chapters I consider these both completed now Added an example or two to the strings chapter TICA10, December 28, 1998 Complete rewrite of the ExtractCode.cpp program to automatically generate makefiles for each compiler that the book tests, excluding files that the compiler can’t handle (these are in a special list in the appendices, so you can see what breaks a compiler, and you can create your own) You now don’t need to extract the files yourself (although you still can, for special cases) but instead you just download and unzip a file All the files in the book (with the exception of the files that are still in Java) now compile with at least one Standard C++ compiler Added the trim.h, SiteMapConvert.cpp and StringCharReplace.cpp examples to the strings chapter Added the ProgVals example to chapter 20 Removed all the strlwr( ) uses (it’s a non-standard function) TICA9, December 15, 1998 Massive work completed on the STL Algorithms chapter; it’s quite close to being finished The long delay was because (1) This chapter took a lot of research and thinking, including other research such as templates; you’ll notice the “advanced templates” chapter has more in it’s outline (2) I was traveling and giving seminars, etc I’m entering a two-month hiatus where I’m primarily working on the book and should get a lot accomplished TICA8, September 26, 1998 Completed the STL containers chapter TICA7, August 14, 1998 Strings chapter modified Other odds and ends TICA6, August 6, 1998 Strings chapter added, still needs some work but it’s in fairly good shape The basic structure for the STL Algorithms chapter is in place and “just” needs to be filled out Reorganized the chapters; this should be very close to the final organization (unless I discover I’ve left something out) TICA5, August 2, 1998: Lots of work done on this version Everything compiles (except for the design patterns chapter with the Java code) under Borland C++ 5.3 This is the only compiler that even comes close, but I have high hopes for the next verison of egcs The chapters and organization of the book is starting to take on more form A lot of work and new material added in the “STL Containers” chapter (in preparation for my STL talks at the Borland and SD conferences), although that is far from finished Also, replaced many of the situations in the first edition where I used my home-grown containers with STL containers (typically vector) Changed all header includes to new style (except for C programs): instead of , instead of , etc Adjustment of namespace issues (“using namespace std” in cpp files, full qualification of names in header files) Added appendix A to describe coding style (including namespaces) Added “require.h” error testing code and used it universally Rearranged header include order to go from more general to more specific (consistency and style issue described in appendix A) Replaced ‘main( ) {}’ form with ‘int main( ) { }’ form (this relies on the default “return 0” behavior, although some compilers, notably VC++, give warnings) Went through and implemented the class naming policy (following the Java/Smalltalk policy of starting with uppercase etc.) but not the member functions/data members (starting with lowercase etc.) Added appendix A on coding style Tested code with my modified version of Borland C++ 5.3 (cribbed a corrected ostream_iterator from egcs and from elsewhere) so not all the programs will compile with your compiler (VC++ in particular has a lot of trouble with namespaces) On the web site, I added the broken-up versions of the files for easier downloads TICA4, July 22, 1998: More changes and additions to the “CGI Programming” section at the end of Chapter 23 I think that section is finished now, with the exception of corrections TICA3, July 14, 1998: First revision with content editing (instead of just being a posting to test the formatting and code extraction process) Changes in the end of Chapter 23, on the “CGI Programming” section Minor tweaks elsewhere RTF format should be fixed now TICA2, July 9, 1998: Changed all fonts to Times and Courier (which are universal); changed distribution format to RTF (readable by most PC and Mac Word Processors, and by at least one on Linux: StarOffice from www.caldera.com Please let me know if you know about other RTF word processors under Linux) ToDo: Fix autobuild of make test makefile (remove backslashes); add test arguments (what about some kind of autofill via redirection?) Differentiate copy-assignment operator= from other forms of operator= HorseRace game as example of random number generator in early chapter? Change header numbering scheme as suggested? The instructions on the web site (http://www.BruceEckel.com/ThinkingInCPP2e.html) show you how to extract code for both Win32 systems and Linux (only Red Hat Linux 5.0/5.1 has been tested) The contents of the book, including the contents of the source-code files generated during automatic code extraction, are not intended to indicate any accurate or finished form of the book or source code Please only add comments/corrections using the form found on http://www.BruceEckel.com/ThinkingInCPP2e.html Please note that the book files are only available in Rich Text Format (RTF) or plain ASCII text without line breaks (that is, each paragraph is on a single line, so if you bring it into a typical text editor that does line wrapping, it will read decently) Please see the Web page for information about word processors that support RTF The only fonts used are Times and Courier (so there should be no font difficulties); if you find any other fonts please report the location Thanks for your participation in this project Bruce Eckel “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++ Bruce Eckel President, MindView Inc Prentice Hall PTR Upper Saddle River, New Jersey 07458 http://www.phptr.com Publisher: Alan Apt Production Editor: Mona Pompilli Development Editor: Sondra Chavez Book Design, Cover Design and Cover Photo: Daniel Will-Harris, daniel@will-harris.com Copy Editor: Shirley Michaels Production Coordinator:Lori Bulwin Editorial Assistant: Shirley McGuire © 1999 by Bruce Eckel, MindView, Inc Published by Prentice Hall Inc A Paramount Communications Company Englewood Cliffs, New Jersey 07632 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 author, 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 Printed in the United States of America 10 ISBN 0-13-917709-4 Prentice-Hall International (UK) Limited, London Prentice-Hall of Australia Pty Limited, Sydney Prentice-Hall Canada, Inc., Toronto Prentice-Hall Hisapnoamericana, S.A., Mexico Prentice-Hall of India Private Limited, New Delhi Prentice-Hall of Japan, Inc., Tokyo Simon & Schuster Asia Pte Ltd., Singapore Editora Prentice-Hall Brasil, Ltda., Rio de Janeiro dedication to the scholar, the healer, and the muse What’s inside Thinking in C++ 2nd edition VERSION TICA17 Preface 17 Prerequisites .17 Thinking in C 17 Learning C++ .18 Goals 19 Chapters 20 Exercises 23 Source code 24 Coding standards 25 Language standards 26 Language support 26 Seminars & CD Roms 26 Errors 27 Acknowledgements 27 1: Introduction to objects 29 The progress of abstraction 29 An object has an interface 31 The hidden implementation 33 Reusing the implementation 34 Inheritance: reusing the interface35 Is-a vs is-like-a relationships 39 Interchangeable objects with polymorphism 40 Creating and destroying objects 43 Exception handling: dealing with errors 44 Analysis and design 45 Phase 0: Make a plan 47 Phase 1: What are we making? 48 Phase 2: How will we build it? 50 Phase 3: Build it 53 Phase 4: Iteration 53 Plans pay off 55 Why C++ succeeds 55 A better C 56 You’re already on the learning curve 56 Efficiency 56 Systems are easier to express and understand 57 Maximal leverage with libraries 57 Source-code reuse with templates 57 Error handling 57 Programming in the large 58 Strategies for transition 58 Guidelines 58 Management obstacles 60 Summary 61 2: Making & using objects 63 The process of language translation63 Interpreters 64 Compilers 64 The compilation process 65 Tools for separate compilation 66 Declarations vs definitions 67 Linking 71 Using libraries 72 Your first C++ program 73 Using the iostreams class 73 Namespaces 74 Fundamentals of program structure 75 "Hello, world!" 76 Running the compiler 77 More about iostreams 77 Character array concatenation 78 Reading input 78 Simple file manipulation 79 Introducing strings 80 Reading and writing files 82 Introducing vector 84 Summary 87 Exercises 88 3: The C in C++ 91 Creating functions 91 Using the C function library 94 Creating your own libraries with the librarian 94 Controlling execution 95 True and false 95 if-else 95 while 96 do-while 97 for 98 The break and continue Keywords 99 switch 100 Recursion 102 Introduction to operators 102 Precedence 103 Auto increment and decrement 103 Introduction to data types .104 Basic built-in types 104 bool, true, & false 105 Specifiers 106 Introduction to Pointers 108 Modifying the outside object 112 Introduction to C++ references 114 Pointers and references as modifiers115 Scoping 117 Defining variables on the fly 118 Specifying storage allocation 120 Global variables 120 Local variables 121 static 121 extern 123 Constants 124 volatile 125 Operators and their use 126 Assignment 126 Mathematical operators 126 Relational operators 128 Logical operators 128 Bitwise operators 129 Shift operators 129 Unary operators 132 The ternary operator 133 The comma operator 133 Common pitfalls when using operators134 Casting operators 134 sizeof – an operator by itself 135 The asm keyword 135 Explicit operators 135 Composite type creation 136 Aliasing names with typedef 136 Combining variables with struct 137 Clarifying programs with enum 140 Saving memory with union 141 Arrays 142 Debugging hints .150 Debugging flags 151 Turning variables and expressions into strings 152 The C assert( ) macro 153 Make: an essential tool for separate compilation 154 Make activities 155 Makefiles in this book 157 An example makefile 158 Summary 160 Exercises 160 4: Data abstraction 163 A tiny C-like library 164 Dynamic storage allocation 167 Bad guesses 170 What's wrong? 171 The basic object 172 What's an object? 178 Abstract data typing 179 Object details 179 Header file etiquette 181 Importance of header files 181 The multiple-declaration problem 183 The preprocessor directives #define, #ifdef and #endif 183 A standard for header files 184 Namespaces in headers 185 Using headers in projects 185 Nested structures 186 Global scope resolution 189 Summary 190 Exercises 190 5: Hiding the implementation 195 Setting limits 195 C++ access control 196 protected 198 Friends 198 Nested friends 200 Is it pure? 203 Object layout 203 The class 204 Modifying Stash to use access control206 Modifying Stack to use access control207 Handle classes 208 Hiding the implementation 208 Reducing recompilation 208 Summary 211 Exercises 211 6: Initialization & cleanup 213 Guaranteed initialization with the constructor 214 Guaranteed cleanup with the destructor 215 Elimination of the definition block217 for loops 219 Storage allocation 220 Stash with constructors and destructors 221 Stack with constructors & destructors 224 Aggregate initialization 227 Default constructors 229 Summary 230 Exercises 231 7: Function overloading & default arguments 233 More name decoration 234 Overloading on return values 235 Type-safe linkage 235 Overloading example .236 unions .239 Default arguments 243 Placeholder arguments 244 Choosing overloading vs default arguments 244 Summary 249 Exercises 249 8: Constants 251 Value substitution .251 const in header files 252 Safety consts 253 Aggregates 254 Differences with C 254 Pointers 255 Pointer to const 256 const pointer 256 Assignment and type checking 257 Function arguments & return values 258 Passing by const value 258 Returning by const value 259 Passing and returning addresses 262 Classes 265 const and enum in classes 265 Compile-time constants in classes 268 const objects & member functions 271 ROMability 275 volatile 275 Summary 277 Exercises 277 9: Inline functions 281 Preprocessor pitfalls 281 Macros and access 284 Inline functions 285 Inlines inside classes 285 Access functions 286 Stash & Stack with inlines 292 Inlines & the compiler 292 Limitations 292 Order of evaluation 293 Hidden activities in constructors & destructors 294 Forward referencing 295 Reducing clutter 295 More preprocessor features 296 Token pasting 297 Improved error checking 297 Summary 300 Exercises 300 10: Name control 303 Static elements from C 303 static variables inside functions 303 Controlling linkage 307 Other storage class specifiers 309 Namespaces 309 Creating a namespace 309 Using a namespace 311 Static members in C++ 315 Defining storage for static data members 315 Nested and local classes 317 static member functions 318 Static initialization dependency 321 What to 322 Alternate linkage specifications324 Summary 325 Exercises 325 11: References & the copyconstructor 327 Pointers in C++ 327 References in C++ 328 References in functions 328 Argument-passing guidelines 331 The copy-constructor 331 lifetime AND, 133 object, 389 explicit bitwise and logical operators, 134 of temporary objects, 331 NOT (!), 131 of variables, 217 operators, 127, 360 limits, implementation, 506 OR, 133 LIMITS.H, 104, 585 long, 105 line input, 554 long double, 124 linkage, 122, 291 longjmp( ), 214, 854 alternate linkage specification, 313 loop, for, 217 controlling, 296 Love, Tom, 1075 external, 246, 247, 296 lvalue, 125, 251 internal, 244, 246, 247, 273, 296 machine instructions, 63 no, 123, 296 type-safe, 232 linked list, 185, 205, 222 linker, 64, 66, 71, 170 collision, 182 macro argument, 270 preprocessor, 127, 269 preprocessor macros for parameterized types, instead of templates, 482 object file order, 72 magic numbers, 243 pre-empting a library function, 72 main( ) searching libraries, 72, 94 executing code after exiting, 295 unresolved references, 72 executing code before entering, 295 list constructor initializer, 257 constructor initializer list, 419 linked, 185, 205, 222 Lister, Timothy, 1075 local classes, 306 static object, 294 local variables, 120 main(), 75 maintaining class library source, 586 make, 153 dependencies, 154 macros, 154 malloc( ), 166, 391, 392, 394, 567, 606 and time, 394 mangling, name, 171, 176, 230, 313 manipulator, 552 localtime( ), 597 creating, 582 logic_error iostreams formatting, 577 Standard C++ library exception type, 874 logical mathematical operators, 125 member 1118 calling a member function, 178 const member function, 256 const member functions, 260 defining storage for static data member, 303 friend function, 196 MI multiple inheritance, 826 minimum size of a struct, 179 mistakes, and design, 208 modes, iostream open, 558 initializing const data members, 257 member function selection, 174 modulus, 125 member function template, 612 modulus operator, 597 member object initialization, 419 monolithic, 826 member selection operator, 176 Mortensen, Owen, 337 overloaded member operator, 342 multiple dispatching, 934 pointers to members, 335 static data member, 377 multiple inclusion of header files, 182 multiple inheritance, 441, 826 static data member inside a class, 303 ambiguity, 829 static member function, 266, 329 and exception handling, 878 static member functions, 307 and run-time type identification, 890, 894, 899 vs non-member operators, 365 and upcasting, 836 memberwise assignment, 378 initialization, 334 memberwise const, 263 memory avoiding, 846 diamonds, 829 duplicate subobjects, 828 most-derived class, 832 overhead, 835 a memory allocation system, 606 pitfall, 842 allocation, 403 repairing an interface, 847 dynamic allocation, 390 upcasting, 829 dynamic memory allocation, 166 virtual base classes, 830 exhausting heap, 407 virtual base classes with a default constructor, 833 management, reference counting, 371 memory manager overhead, 394 multiplication, 125 read-only (ROM), 264 multitasking and volatile, 265 simple storage allocation system, 406 Murray, Rob, 367, 1053 memset( ), 258 message, sending, 178, 452 mutable, 907 bitwise vs memberwise const, 263 methodology, software development, 46 mutators, 275 Meyers, Scott, 1053 naked pointers, and exception handling, 868 1119 name clashes, 170 decoration, 176, 230 file, 1047 hiding, during inheritance, 424 mangling, 171, 176, 230, 313 mangling, no standard for, 231 operator new and constructor, out of memory, 410 operator new placement specifier, 411 operator, exhausting storage, 402 overloaded, can take multiple arguments, 411 overloading array new and delete, 867 overloading global new and delete, 404 overloading new and delete, 403 named constant, 123 namespace, 297, 585 aliasing, 298 ambiguity, 301 continuation, 298 injection, 299 overloading and using declaration, 302 referring to names in, 299 overloading new and delete for a class, 406 overloading new and delete for arrays, 408 placement syntax, 867 newline, 582 no linkage, 123, 296 non-local goto, 214 setjmp( ) and longjmp( ), 854 unnamed, 299 not equivalent (!=), 127 using, 299 not, ! (logical NOT), 134 using declaration, 302 not_eq, != (logical not-equivalent), 134 narrowing conversions, 906 notifyObservers( ), 924, 926 needless recompilation, 205 nuance, and overloading, 229 nested null references, 893 class, 306 friends, 198 structures, 185 nested scopes, 116 network programming NULL references, 318 numerical operations efficiency using the Standard C++ Numerics library, 507 object, 30, 65 CGI POST, 1018 address of, 197 CGI programming in C++, 1014 const member functions, 260 connecting Java to CGI, 1012 creating a new object from an existing object, 326 crash course in CGI programming, 1012 creating on the heap, 394 new, 131, 567 and delete for arrays, 401 new-expression, 392, 403 new-handler, 402, 407 operator, 392 definition point, 212 destruction of static, 294 global constructor, 294 going out of scope, 116 layout, and access control, 200 1120 lifetime, 389 smart pointer, 362 object-oriented programming, 884 ->*, 362 passing and returning large, 322 >>, 551 size, 394 and bool, 105 size, non-zero forcing, 454 assignment, 359 slicing, 464, 467 binary, 128 slicing, and exception handling, 872, 874 binary overloaded, 342 static initialization dependency, 309 binary overloading examples, 348 temporary, 252, 331, 585 bitwise, 128 object module, 65 C & C++, 102 object-oriented casting, 133 analysis & design, 45 choosing between member and non-member overloaded, guidelines, 367 Observable, 924 comma, 132, 361 observer design pattern, 924 common pitfalls, 133 oct, 577 explicit bitwise and logical operators, 134 octal, 124 fan-out in automatic type conversion, 385 off-by-one error, 225 ofstream, 551, 556 as a static object, 295 ones complement operator, 128 OOP, 201 summarized, 178 global overloaded, 342 global scope resolution, 188 increment and decrement, 360 logical, 127, 360 member selection, 176 member vs non-member, 365 modulus, 597 open modes, iostreams, 558 new, 392 operator, 125 new placement specifier, 411 ( ), function call, 362 new, exhausting storage, 402 [], 361, 396, 484, 871 new-expression, 392 ++, 348 no exponentiation, 365 ), 128 reinterpret_cast, 903, 907 Rogue Wave, 503 relational operators, 127 ROM reporting errors in book, 27 requirements analysis, 48 resolution read-only memory, 264 ROMability, 264 root, 878 global scope, 188 rotate, 130 scope, 207 RTTI resolving references, 66 resumption, 861 termination vs resumption, exception handling, 858 re-throwing an exception, 862 return by value as const, 360 constructor return value, 213 efficiency when creating and returning objects, 360 operator overloading arguments and return values, 359 misuse of RTTI, 961, 977 run-time type identification (RTTI), 467 run-time binding, 448 run-time debugging flags, 150 run-time interpreter for printf( ), 548 run-time type identification, 467, 506, 842, 883 and efficiency, 896 and exception handling, 884 and multiple inheritance, 890, 894, 899 and nested classes, 889 and references, 893 overloaded operator return type, 343 and templates, 891 overloading on return values, 231 and upcasting, 884 passing and returning by value, C, 321 and void pointers, 891 passing and returning large objects, 322 bad_cast, 893 return by value, 317 bad_typeid, 894 return value semantics, 254 before( ), 885 returning by const value, 251 building your own, 899 returning references to local objects, 319 casting to intermediate levels, 890 RETURN assembly-language, 323 difference between dynamic_cast and typeid( ), 891 dynamic_cast, 885 return value, void, 93 returning a value from a function, 93 reuse code reuse, 415 mechanism & overhead, 899 misuse, 895 RTTI, abbreviation for, 884 shape example, 883 1125 typeid( ), 884 check for in operator overloading, 359 typeid( ) and built-in types, 888 semantics, return value, 254 typeinfo, 884, 899 sending a message, 178, 452 type-safe downcast, 885 separate compilation, 64, 153 vendor-defined, 884 VTABLE, 899 when to use it, 895 without virtual functions, 884, 889 run-time, access control, 205 runtime_error Standard C++ library exception type, 874 separate compilation, 66 separation of interface & implementation, 194 separation of interface and implementation, 34, 201 sequence point, 212, 218 serialization, 597 and persistence, 839 rvalue, 125 Saks, Dan, 283, 1053 scheduling, software development, 50 Schwarz, Jerry, 311, 583 scope, 214, 394 file, 247, 296 of static member initialization, 304 resolution, 207 resolution operator, 172, 307 resolution operator, for calling base-class functions, 419 resolution, global, 188 scoping, 116 and storage allocation, 390 and the preprocessor, 272 consts, 245 security, 205 sed, 585 seekg( ), 561 seeking in iostreams, 560 seekp( ), 561 selection, member function, 174 self-assignment set STL set class example, 634 set_new_handler, 506 set_terminate( ), 863 set_unexpected( ) exception handling, 859 setChanged( ), 926 setf( ), iostreams, 570, 595 setjmp( ), 214, 854 setw( ), 595 shape example, 1063 example, and run-time type identification, 883 hierarchy, 474 shift operators, 128 short, 105 side effect, 125, 131 signal( ), 854, 875 signed, 105 simple file manipulation, 79 Simula-67, 201 simulating virtual constructors, 1063 1126 single-precision floating point, 104 pointer, 291 singleton, 918 push-down, 205 size object, 394 stash and stack as templates, 490 standard of a struct, 178 Standard C, 26 of object, nonzero forcing, 454 Standard C++, 26 size_t, 404 Standard C++ libraries sizeof, 179, 842 algorithms library, 507 storage, 164 bit_string bit vector, 507 size, built-in types, 103 sizeof, 134 slicing bits bit vector, 507 complex number class, 507 containers library, 507 diagnostics library, 506 object slicing, 467 general utilities library, 506 object slicing and exception handling, 872, 874 iterators library, 507 Smalltalk, 30, 66, 480, 826 language support, 506 smart pointer operator->, 362, 498, 502 localization library, 507 sort numerics library, 507 bubble sort, 618 standard exception classes, 506 source-level debugger, 65 standard library exception types, 874 specialization, 430 standard template library (STL), 633 template specialization, 620 specification string class, 551 standard for each class header file, 183 exception, 858 standard input, 78 incomplete type, 197, 206 standard library, 73 system specification, 48 standard output, 74 specifier standard template library access, 194 operations on, with algorithms, 507 access specifiers, 34 set class example, 634 order for access, 195 specifiers, 105 specifying storage allocation, 119 stack, 185, 219, 390 startup module, 73 stash stash and stack as templates, 490 static, 120, 291 a string class on the stack, 612 array initialization, 305 function-call stack frame, 323 const inside class, 306 1127 data area, 291 static storage class specifier, 296 data member, 377 storage allocation functions for the STL, 506 data members inside a class, 303 storage class, 296 defining storage for static data members, 303 storing type information, 453 destruction of objects, 294 str( ), strstream, 567 downcast, 906 file, 297 stream, 551 output formatting, 569 initialization dependency, 309 initialization to zero, 310 initializer for a variable of a built-in type, 293 local object, 294 streambuf, 559 and get( ), 560 streampos, moving, 560 member function, 266, 329 stricmp( ), non-Standard C function, 614 member functions, 307 string, 76 storage, 291 a string class on the stack, 612 storage area, 390 literals, 249 variables inside functions, 291 preprocessor string concatenation, 284 static type checking, 66 Standard C++ library string class, 551 static_cast, 903, 904 string class example, 383 stdio, 545 transforming character strings to typed values, 564 STDIO.H, 556 turning variable name into, 151 Stepanov, Alexander, 633 STL C++ Standard Template Library, 1014 standard template library, 633 storage String indexOf( ), 957 substring( ), 957 string concatenation, 78 stringizing, 151 allocation, 218 stringizing, preprocessor, 284, 575 auto storage class specifier, 297 strncpy( ) defining storage for static data members, 303 extern storage class specifier, 296 Standard C library function strncpy( ), 866 strongly typed language, 317 register storage class specifier, 297 running out, 402 Stroustrup, Bjarne, 18, 25, 310, 482 simple allocation system, 406 strstr( ), 590 sizes, 164 strstream, 428, 563, 590 static, 291 automatic storage allocation, 566 static area, 390 ends, 565 1128 freezing, 567 and inheritance, 615 output, 565 and multiple definitions, 486 str( ), 567 and run-time type identification, 891 user-allocated storage, 563 argument list, 487 zero terminator, 565 C++ Standard Template Library (STL), 1014 strtok( ) Standard C library function, 683 struct minimum size, 179 size of, 178 constants in templates, 488 container class templates and virtual functions, 502 controlling instantiation, 620 creating specific template types, 620 function templates, 606 structural design patterns, 922 generated classes, 484 structure in C++, 969 aggregate initialization and structures, 226 instantiation, 484 C, 177 member function template, 612 declaration, 197 preprocessor macros for parameterized types, instead of templates, 482 declaring, 182 friend, 196 nested, 185 redeclaring, 182 subobject, 416, 418, 419, 426 preventing template bloat, 618 requirements of template classes, 617 specialization, 620 standard template library (STL), 633 stash and stack as templates, 490 duplicate subobjects in multiple inheritance, 828 substitution, value, 243 temporary object, 252, 331, 585 substring( ), 957 subtraction, 125 subtyping, 428 sugar, syntactic, 341 switch, 100, 218 system specification, 48 system() function, 80 tag name, 163 passing a temporary object to a function, 255 temporary objects and function references, 320 terminate( ), 506 uncaught exceptions, 862 termination vs resumption, exception handling, 858 terminator zero for strstream, 565 tellg( ), 560 ternary operator, 132 tellp( ), 560 this, 174, 263, 307, 331, 392, 456 template throwing an exception, 856 and header files, 485 time, Standard C library, 278 1129 token pasting, preprocessor, 284 dynamic, 66 toupper( ), 586 static, 66 unexpected results, 272 type-checking, 68 trace information, adding to program, 374 typedef, 172 transforming character strings to typed values, 564 typeid( ) translation unit, 169, 309 true, 127, 131, 133, 183 and built-in types, run-time type identification, 888 and exceptions, 894 true and false, bool, 104 difference between dynamic_cast and typeid( ), run-time type identification, 891 try block, 857 run-time type identification, 884 tuple-making template function, 506 type typeinfo run-time type identification, 884 automatic type conversion, 379 structure, 899 automatic type conversions and exception handling, 872 TYPEINFO.H, 892 basic built-in, 103 ULONG_MAX, 585 unary built-in types and typeid( ), run-time type identification, 888 examples of all overloaded unary operators, 343 checking, 123 minus (-), 131 conversion, 170 operator, 128 finding exact type of a base pointer, 884 operators, 131 function type, 280 overloaded unary operators, 342 implicit conversion, 124 plus (+), 131 improved type checking, 175 uncaught exceptions, 862 incomplete type specification, 197, 206 unexpected( ), 506 new cast syntax, 903 preventing automatic type conversion with the keyword explicit, 380 run-time type identification (RTTI), 467, 883 storing type information, 453 exception handling, 859 union anonymous at file scope, 239 saving memory with, 140 type checking for enumerations, 260 unions, additional type checking, 260 type checking for unions, 260 unit buffering, iostream, 571 type-safe downcast in run-time type identification, 885 Unix, 585 type-safe linkage, 232 type checking, 66 unnamed arguments, 92 unnamed namespace, 299 unsigned, 105 1130 untagged enum, 258 variable argument list, 92 unusual operator overloading, 361 variable declaration syntax, 68 upcasting, 435, 446, 452 variance, 1055 and multiple inheritance, 829, 836 vector of change, 54, 918, 950, 980 and run-time type identification, 884 vendor-defined run-time type identification, 884 by value, 457 Urlocker, Zack, 851 use case, 48 user-defined data type, 103 user-defined type, 63 user-defined types, 178 virtual abstract base classes and pure virtual functions, 460 adding new virtual functions in the derived class, 465 and efficiency, 458 and late binding, 453 using declaration, for namespaces, 302 keyword, namespaces, 299 using iostreams, 73 assembly-language code generated by a virtual function, 456 behavior of virtual functions inside constructors, 471 destructor, 494, 502 using libraries, 72 value destructors and virtual destructors, 472 function, 446, 502 preprocessor value substitution, 243 function overriding, 448 transforming character strings to typed values, 564 keyword, 448 values, minimum and maximum, 103 variable picturing virtual functions, 454 pure virtual function definitions, 464 automatic, 123 run-time type identification without virtual functions, 884, 889 defining, 117 simulating virtual constructors, 1063 file scope, 121 size overhead of virtual functions, 453 global, 119 virtual base classes, 830 going out of scope, 116 virtual base classes with a default constructor, 833 initializer for a static variable of a built-in type, 293 virtual function calls in destructors, 473 lifetime of variables, 217 virtual functions inside constructors, 469, 1063 local, 120 virtual keyword in derived-class declarations, 460 point of definition, 215 virtual memory, 392 register, 120 visibility, 291 turning name into a string, 151 visitor pattern, 937 variable argument list, 549 void, 93 1131 argument list, 92 Waldrop, M Mitchell, 1075 casting void pointers, 175 while, 96 pointer, 164, 317, 395, 396, 398 void pointers and run-time type identification, 891 volatile, 124, 265 casting away const and/or volatile, 904 wild-card, 46 wrapping, class, 545 write( ), 555 iostream read( ) and write( ), 841 vpointer, abbreviated as VPTR, 453 ws, 578 VPTR, 453, 455, 457, 469, 471, 842, 1064 XOR, 128 installing by constructor, 457 VTABLE, 453, 455, 457, 461, 466, 469, 471, 1064 and run-time type identification, 899 xor, ^ (bitwise exclusive-OR), 134 xor_eq, ^= (bitwise exclusive-OR-assignment), 134 zero terminator, strstream, 565 inheritance and the VTABLE, 465 1132 ... C++ Library 515 Library overview 516 17: Strings 519 What’s in a string 519 Creating and initializing C++ strings521 Operating on strings 523 Appending, inserting and concatenating strings... 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... Reading input 78 Simple file manipulation 79 Introducing strings 80 Reading and writing files 82 Introducing vector 84 Summary 87 Exercises 88 3: The C in C++ 91 Creating

Ngày đăng: 19/03/2014, 14:06

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w