C++ in a nutshell

810 149 0
C++ in a nutshell

Đ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

C++ IN A NUTSHELL C++ IN A NUTSHELL Ray Lischner Beijing • Cambridge • Farnham • Kưln • Paris • Sebastopol • Taipei • Tokyo C++ in a Nutshell by Ray Lischner Copyright © 2003 O’Reilly Media, Inc All rights reserved Printed in the United States of America Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472 O’Reilly Media, Inc books may be purchased for educational, business, or sales promotional use Online editions are also available for most titles (safari.oreilly.com) For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com Editor: Jonathan Gennick Production Editor: Matt Hutchinson Cover Designer: Ellie Volckhausen Interior Designer: David Futato Printing History: May 2003: First Edition Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc The In a Nutshell series designations, C++ in a Nutshell, the image of an Eastern chipmunk, and related trade dress are trademarks of O’Reilly Media, Inc Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks Where those designations appear in this book, and O’Reilly Media, Inc was aware of a trademark claim, the designations have been printed in caps or initial caps While every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein This book uses RepKover™, a durable and flexible lay-flat binding ISBN-10: 0-596-00298-X ISBN-13: 978-0-596-00298-5 [M] [5/07] Chapter Table of Contents Preface ix Language Basics Compilation Steps Tokens Comments Character Sets Alternative Tokens Trigraphs 8 10 Declarations 12 Declarations and Definitions Scope Name Lookup Linkage Type Declarations Object Declarations Namespaces 12 14 16 22 24 29 42 Expressions 50 Lvalues and Rvalues Type Conversions Constant Expressions Expression Evaluation Expression Rules 50 52 56 57 59 v This is the Title of the Book, eMatter Edition Copyright © 2007 O’Reilly & Associates, Inc All rights reserved Statements 83 Expression Statements Declarations Compound Statements Selections Loops Control Statements Handling Exceptions 83 84 86 87 89 92 94 Functions 98 Function Declarations Function Definitions Function Overloading Operator Overloading The main Function 98 106 109 124 130 Classes 132 Class Definitions Data Members Member Functions Inheritance Access Specifiers Friends Nested Types 132 139 142 155 167 170 172 Templates 174 Overview of Templates Template Declarations Function Templates Class Templates Specialization Partial Specialization Instantiation Name Lookup Tricks with Templates Compiling Templates 175 177 180 186 191 194 195 199 205 208 Standard Library 211 Overview of the Standard Library C Library Wrappers Wide and Multibyte Characters Traits and Policies vi | Table of Contents This is the Title of the Book, eMatter Edition Copyright © 2007 O’Reilly & Associates, Inc All rights reserved 211 215 215 217 Allocators Numerics 223 225 Input and Output 229 Introduction to I/O Streams Text I/O Binary I/O Stream Buffers Manipulators Errors and Exceptions 229 235 237 237 241 243 10 Containers, Iterators, and Algorithms 246 Containers Iterators Algorithms 246 261 266 11 Preprocessor Reference 276 12 Language Reference 290 13 Library Reference 327 328 369 375 376 378 380 384 384 386 390 397 406 407 410 412 413 429 439 445 450 465 Table of Contents | This is the Title of the Book, eMatter Edition Copyright © 2007 O’Reilly & Associates, Inc All rights reserved vii 470 475 478 487 503 504 523 525 527 535 553 558 564 602 613 623 627 629 634 638 647 655 657 660 667 686 693 695 698 720 A Compiler Extensions 729 B Projects 735 Glossary 741 Index 745 viii | Table of Contents This is the Title of the Book, eMatter Edition Copyright © 2007 O’Reilly & Associates, Inc All rights reserved rebind function (allocator class template), 616 records versus classes, 135 recursion, examples, 205–207 recursive functions, 100 reference function (allocator class template), 616 reference sections language, 290–326 preprocessor, 276–289 reference type and containers, 249 references, 35–38 binding to conversions, 112, 119 common uses of, 36 const (see const reference) initializing, 36 restrictions on, 37 versus pointers, 35 register storage class, 314 example, 314 specifier, 30 register_callback function (ios_base class), 513 reinterpret_cast operator, 55, 65, 315 examples, 66, 315 pointers-to-members and, 144 restrictions, 66 unions and, 138 relational expressions, 77 relational operators in containers, 251 release function (auto_ptr class template), 620 rel_ops namespace ( header), 698 remainders, 75 remove function ( header), 424 (list class template), 562 remove function template ( header), 272, 353 remove_copy function template ( header), 272, 354 remove_copy_if function template ( header), 272, 354 remove_if function (list class template), 562 remove_if function template ( header), 272, 355 780 | rename function ( header), 425 rend function (deque class template), 474 (list class template), 562 (map class template), 607 (multimap class template), 611 (multiset class template), 641 (set class template), 646 (valarray class template), 725 replace function (basic_string class template), 677 replace function template ( header), 272, 355 replace_copy function template ( header), 272, 356 replace_copy_if function template ( header), 272, 356 replace_if function template ( header), 272, 357 reserve function (basic_string class template), 678 (valarray class template), 725 reserved identifiers, reserved keywords, list of, reset function (auto_ptr class template), 620 (bitset class template), 372 resetioflags function, 241 resetiosflags function ( header), 503 resize function (basic_string class template), 678 (deque class template), 474 (list class template), 562 (valarray class template), 720, 725 Resource Allocation Is Initialization (RAII) (glossary definition), 742 restricted classes, 136 return statement, 94, 315 return types covariant, 160 function, 99 void, 100 Return Value Optimization (RVO) (glossary definition), 743 Index This is the Title of the Book, eMatter Edition Copyright © 2007 O’Reilly & Associates, Inc All rights reserved return_temporary_buffer function template ( header), 622 reverse function (list class template), 563 reverse function template ( header), 272, 357 reverse iterators containers and, 265 problems with, 265 reverse_copy function template ( header), 272, 358 reverse_iterator class template ( header), 550 rewind function ( header), 425 rfind function (basic_string class template), 678 right function, 241 ( header), 521 right literal, 516 rotate function template ( header), 272, 358 rotate_copy function template ( header), 272, 359 round_error function (numeric_limits class template), 557 round_style member (numeric_limits class template), 558 runtime character set, runtime_error class ( header), 660 rvalue pointer, 145 rvalues, 50–52 glossary definition, 743 operators and, 51 references and, 36 rules for, 51 RVO (glossary definition), 743 S sbumpc function (basic_streambuf class template), 664 scalars, initializing, 42 scanf function ( header), 232 scan_is function, 571 (ctype class template), 573 scan_not function, 571 (ctype class template), 573 SCHAR_MAX macro ( header), 385 SCHAR_MIN macro ( header), 386 scientific function, 241 ( header), 521 scientific literal, 516 scope linkage, 22 scope operator (::), 17 scopes, 14 named and unnamed, 15 named (see namespaces) nested, 15 search function template ( header), 271, 359 search_n function template ( header), 271, 360 SEEK_CUR macro ( header), 425 seekdir type, 518 SEEK_END macro ( header), 425 seekg function (basic_istream class template), 532 seekoff function (basic_filebuf class template), 481 (basic_streambuf class template), 666 (basic_stringbuf class template), 651 (strstreambuf class), 692 seekp function (basic_ostream class template), 630 seekpos function (basic_filebuf class template), 481 (basic_streambuf class template), 666 (basic_stringbuf class template), 652 (strstreambuf class), 692 SEEK_SET macro ( header), 425 selection statements, 87 sequence containers adding items to, 258 operator< in, 258 operator== in, 258 optional functions, 252 required constructors, 250 value type requirements, 258 sequence points, 57 Index | This is the Title of the Book, eMatter Edition Copyright © 2007 O’Reilly & Associates, Inc All rights reserved 781 sequences accessing any index in, 261 binary search, 271 comparing, 270 merging, 273 modifying, 272–273 searching for values, 271 sorted, set operations, 274 set class template ( header), 247, 642 set function (bitset class template), 372 header, 214, 638–646 multiset class template, 638 operator!= function template, 642 operator< function template, 642 operator function template, 642 operator>= function template, 642 set class template, 642 swap function template, 646 setbase function, 241 ( header), 503 setbuf function (basic_filebuf class template), 481 (basic_streambuf class template), 666 (basic_stringbuf class template), 652 ( header), 426 (strstreambuf class), 692 set_difference function template ( header), 274, 360 setf function (ios_base class), 514 setfill function, 241 setfill function template ( header), 504 setg function (basic_streambuf class template), 666 set_intersection function template ( header), 274, 361 setioflags function, 241 setiosflags function ( header), 504 setjmp function ( header), 406 setlocale function ( header), 390 782 | set_new_handler function ( header), 626 setp function (basic_streambuf class template), 666 setprecision function, 241 ( header), 504 setstate function (basic_ios class template), 507 set_symmetric_difference function template ( header), 274, 362 set_terminate function ( header), 476 set_unexpected function ( header), 477 set_union function template ( header), 274, 363 setvbuf function ( header), 426 setw function, 241 ( header), 504 SFINAE (Substitution Failure Is Not An Error) (glossary definition), 743 sgetc function (basic_streambuf class template), 664 sgetn function (basic_streambuf class template), 664 shared_ptr< > smart pointer template, 738 shift expressions, 76 shift function (valarray class template), 720 shift operators and formatted I/O, 231 shift state, 217 short type, 315 short-circuit evaluation, 59 short-circuit operators, 126 showbase function, 241 ( header), 521 showbase literal, 516 showmanyc function (basic_filebuf class template), 482 (basic_streambuf class template), 664 showpoint function, 241 ( header), 521 showpoint literal, 516 showpos function, 241 ( header), 522 showpos literal, 516 Index This is the Title of the Book, eMatter Edition Copyright © 2007 O’Reilly & Associates, Inc All rights reserved SHRT_MAX macro ( header), 386 SHRT_MIN macro ( header), 386 SIGABRT macro ( header), 408 sig_atomic_t type ( header), 409 SIG_DFL macro ( header), 407 SIG_ERR macro ( header), 408 SIGFPE macro ( header), 408 SIG_IGN macro ( header), 408 SIGILL macro ( header), 408 SIGINT macro ( header), 408 signal function ( header), 409 signaling_NaN function (numeric_limits class template), 558 signed char type, 24 signed int type, 25 signed long int type, 25 signed short int type, 25 signed specifier, 316 SIGSEGV macro ( header), 408 SIGTERM macro ( header), 408 simple classes (see POD) sin function ( header), 396 sin function template ( header), 405 ( header), 713 sinh function ( header), 396 sinh function template ( header), 405 ( header), 713 size function, 251 (basic_string class template), 678 (bitset class template), 373 (deque class template), 474 (list class template), 563 (map class template), 607 (multimap class template), 611 (multiset class template), 641 (priority_queue class template), 636 (queue class template), 637 (set class template), 646 (stack class template), 657 (valarray class template), 720, 725 sizeof operator, 71, 316 example, 71, 316 size_t type ( header), 413 ( header), 426 ( header), 436 ( header), 441 ( header), 448 ( header), 455 size_type type (allocator class template), 617 size_type type and containers, 249 skipws function, 241 ( header), 522 skipws literal, 516 slice class ( header), 713 slice_array class template ( header), 714 slicing an object, 156 slist container example, 254–258 snextc function (basic_streambuf class template), 664 sort function (list class template), 563 sort function template ( header), 273, 364 sort_heap function template ( header), 274, 365 sorting, 273 source character set, specialization, explicit (glossary definition), 741 specialization (glossary definition), 743 specializing templates, 175, 191–195 class, 192 examples, 193 function, 191 partial, 194 example, 195 specifiers, 30–31 access (see access specifiers) labels, access (see access specifiers, labels) using, 31 splice function (list class template), 563 sprintf function ( header), 426 sputbackc function (basic_streambuf class template), 664 sputc function (basic_streambuf class template), 664 Index | This is the Title of the Book, eMatter Edition Copyright © 2007 O’Reilly & Associates, Inc All rights reserved 783 sputn function (basic_streambuf class template), 664 sqrt function ( header), 396 sqrt function template ( header), 405 ( header), 716 srand function ( header), 437 sscanf function ( header), 426 header, 214, 234, 647–655 basic_istringstream class template, 648 basic_ostringstream class template, 649 basic_stringbuf class template, 650 basic_stringstream class template, 652 istringstream class, 653 ostringstream class, 653 stringbuf class, 654 stringstream class, 654 wistringstream class, 654 wostringstream class, 654 wstringbuf class, 654 wstringstream class, 655 stable_partition function template ( header), 273, 365 stable_sort function template ( header), 273, 366 stack class template ( header), 248, 656 stack, execution, 95 header, 214, 655–657 operator!= function template, 655 operator< function template, 655 operator function template, 656 operator>= function template, 656 stack class template, 656 standard conversion sequence, 122 standard library, 211–228 C library wrappers, 215 overview, 211–215 policies (see policies) traits (see traits) using, 212 state mask, 244 784 | statements, 83–97, 316 compound, 86 declaration, 84 expression, 83 loop (see loops) mixed with declarations, 84 null, 83 selection, 87 static data members, 141 specializing, 192 static member functions defined, 145 pointers to, 34 this pointers and, 145 static objects, 42 static specifier, 142 static storage class, 139, 317 example, 317 specifier, 30 static_cast operator, 55, 67, 317 examples, 67, 317 statistics with a functor, computing, 269 std namespace, 211 (see also standard library) _ _STDC_ _ macro, 280 stderr macro ( header), 427 header, 214, 657–660 domain_error class, 658 invalid_argument class, 658 length_error class, 658 logic_error class, 658 out_of_range class, 659 overflow_error class, 659 range_error class, 659 runtime_error class, 660 underflow_error class, 660 stdin macro ( header), 427 stdout macro ( header), 427 STLport project, 247, 739 storage class specifiers, 30 friends and, 170 stossc function (basic_streambuf class template), 664 str function (basic_istringstream class template), 649 (basic_ostringstream class template), 650 (basic_stringbuf class template), 652 Index This is the Title of the Book, eMatter Edition Copyright © 2007 O’Reilly & Associates, Inc All rights reserved (istrstream class), 687 (ostrstream class), 688 (strstream class), 689 (strstreambuf class), 691 strcat function ( header), 441 strchr function ( header), 441 strcmp function ( header), 441 strcoll function ( header), 442 strcpy function ( header), 442 strcspn function ( header), 442 stream buffers, 230, 237–241 characterized by, 238 stream class templates, 230 streambuf class ( header), 667 header, 214, 234, 660–667 basic_streambuf class template, 660 streambuf class, 667 wstreambuf class, 667 streamoff type ( header), 522 streams copying one character at a time, 237 copying streams with streambuf iterators, 263 copying via stream buffers, 238 copying with explicit buffers, 238 streamsize type ( header), 522 strerror function ( header), 442 strftime function ( header), 448 string class ( header), 685 header, 214, 667–686 basic_string class template, 669 char_traits class template, 679 char_traits class, 679 char_traits class, 681 getline function template, 681 operator!= function template, 683 operator+ function template, 682 operator< function template, 683 operator= function template, 684 operator>> function template, 685 string class, 685 swap function template, 686 wstring class, 686 string literals, string templates, 248 stringbuf class ( header), 654 stringify operator, 277 stringstream class ( header), 654 strlen function ( header), 443 strncat function ( header), 443 strncmp function ( header), 443 strncpy function ( header), 443 strpbrk function ( header), 443 strrchr function ( header), 444 strspn function ( header), 444 strstr function ( header), 444 strstream class ( header), 688 header, 214, 234, 686–693 istrstream class, 686 ostrstream class, 687 strstream class, 688 strstreambuf class, 689 strstreambuf class ( header), 689 strtod function ( header), 437 strtok function ( header), 444 strtol function ( header), 437 strtoul function ( header), 438 struct keyword, 132, 318 C versus C++, 135 example, 318 strxfrm function ( header), 445 Substitution Failure Is Not An Error (SFINAE) (glossary definition), 743 substr function (basic_string class template), 679 subtraction, 75 sum function (valarray class template), 720 sungetc function (basic_streambuf class template), 664 swap function, 251 (basic_string class template), 679 (deque class template), 474 (list class template), 563 (map class template), 607 (multimap class template), 611 Index | This is the Title of the Book, eMatter Edition Copyright © 2007 O’Reilly & Associates, Inc All rights reserved 785 swap function (continued) (multiset class template), 641 (set class template), 646 (valarray class template), 725 (vector< > template), 726 swap function template ( header), 366 ( header), 475 ( header), 564 ( header), 613 ( header), 646 ( header), 686 ( header), 722 swap_ranges function template ( header), 273, 366 switch statements, 87–89, 318 case and default labels, 88 declaration, 86 example, 318 swprintf function ( header), 456 swscanf function ( header), 456 symbols, sync function (basic_filebuf class template), 482 (basic_istream class template), 532 (basic_streambuf class template), 666 sync_with_stdio function (ios_base class), 514 syntax, expression, 59 system function ( header), 438 T table function (ctype< > class template), 573 tables, virtual function, 157 tan function ( header), 396 tan function template ( header), 406 ( header), 716 function ( header), 397 function template ( header), 406 ( header), 716 tellg function (basic_istream class template), 532 786 | tellp function (basic_ostream class template), 631 template arguments, 181–184 deducing, 182–184 template declarations, 177–180 template headers, 177 template keyword, 318 example, 319 explicitly instantiating a template, 197 template parameters, 177 template, 179 type, 179 value, 178 template-based programming, 174 templates, 174–210 bitset, 141 class (see class templates) compiling, 208–210 container, implementing your own, 221 declarations versus specializations, 175 declaring and defining, 209 defined, 174 examples, 175–177 function (see function templates) instantiation, 175, 195–199 example, 197 explicit, 197 member (see member templates) name lookup and, 199–205 dependent names, 199–201 hiding names, 203 resolving names, 201–203 type names, 204 operator, 185 overload resolution and, 184 overview, 175–177 parameters, 178 specializing (see specializing templates) tricks with, 205–208 wrapper, 37 temporary objects, binding names to, 35 terminate function ( header), 477 test function (bitset class template), 373 testing for I/O success, 245 text I/O, 235–237 this expression, defined, 61 Index This is the Title of the Book, eMatter Edition Copyright © 2007 O’Reilly & Associates, Inc All rights reserved this keyword, 319 examples, 145, 320 this pointers and static member functions, 145 thousands_sep function (moneypunct class template), 590 thousands_sep (lconv member), 389 _ _thread keyword (RAD extension), 730 throw expressions, 81, 94 throw operator, 320 example, 320 tie function (basic_ios class template), 508 tilde (˜),destructor names, 151 time and dates, 450 time function ( header), 449 _ _TIME_ _ macro, 280 time_base class ( header), 597 time_get class template ( header), 597 time_get_byname class template ( header), 599 time_put class template ( header), 599 time_put_byname class template ( header), 601 time_t type ( header), 449 tinyness_before member (numeric_ limits class template), 558 tm struct ( header), 449 ( header), 456 tmpfile function ( header), 427 TMP_MAX macro ( header), 427 tmpnam function ( header), 428 to_char_type function (char_ traits class), 680 to_double member function, 139 to_int_type function (char_traits class), 680 tokens, 2–7 alternative, differences between preprocessor token and compiler token, tolower function, 571 ( header), 378 tolower function template ( header), 601 top function (priority_queue class template), 636 (stack class template), 657 to_string function (bitset class template), 373 to_ulong function (bitset class template), 373 toupper function, 572 ( header), 378 toupper function template ( header), 602 towctrans function ( header), 468 towlower function ( header), 468 towupper function ( header), 468 trait templates, commonly used, 218 traits, 217–223 character, 218–220 custom, 221–223 iterator, 220 transform algorithm, 273 transform function, 569 transform function template ( header), 367 traps member (numeric_limits class template), 558 trigraphs, defined, 10 trivial classes, 136 true literal, 320 trunc literal, 518 try statements, 94–97, 320 examples, 96, 321 two-phase lookup, 199 type, 321 casts, 54 conversions, 52–55 arithmetic, 53 explicit, 54 declarations, 24–29 lists, 206–208 membership, testing, 207 simple uses of, 208 polymorphism (see polymorphism) promotions, 52, 122 specifiers, 31 fundamental, 24 type polymorphism (glossary definition), 743 Index | This is the Title of the Book, eMatter Edition Copyright © 2007 O’Reilly & Associates, Inc All rights reserved 787 typedef declarations, 28, 107 language linkage and, 23 typedef keyword, 322 example, 322 typeid operator, 68, 322 example, 322 type_info class ( header), 693 header, 214, 693–695 bad_cast class, 693 bad_typeid class, 693 type_info class, 693 typename keyword, 29, 179, 199, 322 declaring type template parameters, 295 example, 322 template declarations and, 204 typeof keyword (GNU compiler), 732 U UCHAR_MAX macro ( header), 386 uflow function (basic_filebuf class template), 482 (basic_streambuf class template), 666 UINT_MAX macro ( header), 386 ULONG_MAX macro ( header), 386 unary expressions, 69–74 unary_function class template ( header), 502 unary_negate class template ( header), 502 uncaught_exception function ( header), 477 #undef directive, 289 macros and, 278 undefined behavior (glossary definition), 743 underflow function (basic_filebuf class template), 482 (basic_streambuf class template), 666 (basic_stringbuf class template), 652 (strstreambuf class), 692 underflow_error class ( header), 660 788 | underscores identifiers starting with, identifiers with two consecutive, unexpected constructor calls, 150 unexpected function, 104 ( header), 477 unformatted I/O, 237 unget function (basic_istream class template), 532 ungetc function ( header), 428 ungetwc function ( header), 456 Unicode characters in source file, uninitialized_copy function template ( header), 622 uninitialized_fill function template ( header), 622 uninitialized_fill_n function template ( header), 622 union keyword, 132, 323 C versus C++, 135 example, 323 unions, 136–138 anonymous (see anonymous unions) C versus C++, 138 versus inheritance, 138 unique function (list class template), 563 unique function template ( header), 273, 367 unique_copy function template ( header), 273, 368 unitbuf function, 241 ( header), 523 unitbuf literal, 516 universal characters, compilers and, unnamed namespace, 44 unqualified function calls, 112, 113–115 unqualified name, 61 unqualified name lookup, 18–20 unsetf function (ios_base class), 514 unshift function (codecvt class template), 567 unsigned char type, 25 unsigned long int type, 25 unsigned short int type, 25 unsigned specifier, 323 example, 323 Index This is the Title of the Book, eMatter Edition Copyright © 2007 O’Reilly & Associates, Inc All rights reserved upper_bound function, 253 (map class template), 607 (multimap class template), 611 (multiset class template), 641 (set class template), 646 upper_bound function template ( header), 271, 369 uppercase function, 241 ( header), 523 uppercase literal, 516 use_facet function template ( header), 602 user-defined conversion sequence, 123 USHRT_MAX macro ( header), 386 using declarations, 46–48, 324 #using directive (Managed C++ extension), 734 using directives, 48, 324 within classes or namespaces, 19 using keyword, 324 example, 324 UTF-32 encoding, 216 header, 215, 695–698 make_pair function template, 695 operator!= function template, 696 operator< function template, 696 operator function template, 697 operator>= function template, 697 pair class template, 697 rel_ops namespace, 698 _ _uuidof operator (RAD extension), 730 V va_arg macro ( header), 411 va_end macro ( header), 411 valarray class template ( header), 716 what should have been, 735 valarray class templates, 248 header, 215, 225, 391, 698–720 abs function template, 699 acos function template, 700 asin function templat, 700 atan function template, 700 atan2 function template, 700 cos function template, 700 cosh function template, 700 exp function template, 701 gslice class, 701 gslice_array class template, 703 indirect_array class template, 705 log function template, 706 log10 function template, 706 mask_array class template, 706 operator!= function template, 711 operator& function template, 709 operator&& function template, 710 operator* function template, 708 operator+ function template, 708 operator- function template, 709 operator/ function template, 708 operator< function template, 711 operator= function template, 712 operator>> function template, 710 operator^ function template, 709 operator| function template, 709 operator|| function template, 710 pow function template, 712 sin function template, 713 sinh function template, 713 slice class, 713 slice_array class template, 714 sqrt function template, 716 tan function template, 716 function template, 716 valarray class template, 716 va_list type ( header), 412 _ _value class (Managed C++ extension), 734 value type containers, 249 requirements in, 258 value_comp function, 253 (map class template), 607 (multimap class template), 611 (multiset class template), 641 (set class template), 646 value_type function (allocator class template), 617 va_start macro ( header), 412 vector class template ( header), 247, 722 Index | This is the Title of the Book, eMatter Edition Copyright © 2007 O’Reilly & Associates, Inc All rights reserved 789 header, 215, 720–727 operator!= function template, 721 operator< function template, 721 operator function template, 721 operator>= function template, 721 swap function template, 722 vector class template, 722 vector class, 726 vector::reference class, 727 vector, inserting numbers in a, 263 vector class ( header), 248, 726 vector::reference class ( header), 727 vfprintf function ( header), 428 vfwprintf function ( header), 457 virtual base classes, 164 virtual destructors, 155 virtual function tables, 157 virtual functions, 100, 156–160 calling, 156 declaring and using, 158–160 default arguments and, 102 overridden, 102 overriding, 156 pure, 161 slicing and, 156 specifiers, 156 virtual inheritance, 164–166 virtual specifier, 142, 324 example, 324 Visual C++ compiler, 732 void keyword, 325 example, 325 void type, 25 volatile objects, 57 volatile qualifier, 325 example, 326 volatile specifiers (see cv-qualifiers) vprintf function ( header), 429 vsprintf function ( header), 429 vswprintf function ( header), 457 vtables, 157 vwprintf function ( header), 457 790 | W wcerr variable ( header), 526 WCHAR_MAX macro ( header), 457 WCHAR_MIN macro ( header), 457 wchar_t type, 25, 325 Unicode UTF-32 encoding and, 216 wcin variable ( header), 527 wclog variable ( header), 527 wcout variable ( header), 527 wcrtomb function ( header), 458 wcscat function ( header), 458 wcschr function ( header), 458 wcscmp function ( header), 458 wcscoll function ( header), 459 wcscpy function ( header), 459 wcscspn function ( header), 459 wcsftime function ( header), 459 wcslen function ( header), 460 wcsncat function ( header), 460 wcsncmp function ( header), 460 wcsncpy function ( header), 460 wcspbrk function ( header), 460 wcsrchr function ( header), 461 wcsrtombs function ( header), 461 wcsspn function ( header), 461 wcsstr function ( header), 462 wcstod function ( header), 462 wcstok function ( header), 462 wcstol function ( header), 462 Index This is the Title of the Book, eMatter Edition Copyright © 2007 O’Reilly & Associates, Inc All rights reserved wcstombs function ( header), 439 wcstoul function ( header), 463 wcsxfrm function ( header), 463 wctob function ( header), 463 wctomb function ( header), 438 wctrans function ( header), 468 wctrans_t type ( header), 469 wctype function ( header), 469 wctype_t type ( header), 470 web page, author’s, 740 well-formed (glossary definition), 743 WEOF macro ( header), 463 ( header), 470 wfilebuf class ( header), 486 wfstream class ( header), 487 while statements, 90, 326 declaration, 86 example, 326 whitespace and symbols, whitespace characters, wide characters, 5, 215–217 converting to multibyte characters, 217 wide string literals, widen function (basic_ios class template), 508 (ctype class template), 572 width function (ios_base class), 514 wifstream class ( header), 487 wint_t type ( header), 463 ( header), 470 wiostream class ( header), 534 wistream class ( header), 535 wistringstream class ( header), 654 wmemchr function ( header), 464 wmemcmp function ( header), 464 wmemcpy function ( header), 464 wmemmove function ( header), 464 wmemset function ( header), 464 wofstream class ( header), 487 wostream class ( header), 634 wostringstream class ( header), 654 wprintf function ( header), 465 wrapper template, 37 write function (basic_ostream class template), 237, 631 ws function, 241 ( header), 535 wscanf function ( header), 465 wstreambuf class ( header), 667 wstring class ( header), 248, 686 wstringbuf class ( header), 654 wstringstream class ( header), 655 X xalloc function (ios_base class), 514 xor (exclusive or), 79 xor operator, 326 example, 326 xor_eq operator, 326 example, 326 xsgetn function (basic_streambuf class template), 667 xsputn function (basic_streambuf class template), 667 Index | This is the Title of the Book, eMatter Edition Copyright © 2007 O’Reilly & Associates, Inc All rights reserved 791 About the Author Ray Lischner began his career as a software developer but dropped out of the corporate rat race to become an author He started using C++ in the late 1980s, working at a company that was rewriting its entire product line in C++ Over the years, he has witnessed the evolution of C++ from cfront to native compilers to integrated development environments to visual, component-based tools Ray has taught C++ at Oregon State University He is the author of Delphi in a Nutshell (O’Reilly) and other books Colophon Our look is the result of reader comments, our own experimentation, and feedback from distribution channels Distinctive covers complement our distinctive approach to technical topics, breathing personality and life into potentially dry subjects The animal on the cover of C++ in a Nutshell is an Eastern chipmunk, a striped ground squirrel found mostly in eastern North America Eastern chipmunks have five dark and two light stripes on their backs, extending from head to rump, and two stripes on their long, bushy tails They are distinguished from other ground squirrels by the white stripes above and below their eyes The coloration of chipmunks throughout North America varies but is quite uniform within regions Chipmunks often make their homes in sparse forests or farms, where they can build the entrances to their lodges in stone walls, broken trees, or thick underbrush A lodge consists of a maze of tunnels leading to a large, leaf-lined nest Chipmunks spend most of the daylight hours outdoors but head for their lodges before nightfall Although they are excellent climbers, chipmunks live primarily on the ground Chipmunks eat nuts, seeds, insects, and occasionally birds’ eggs Like all ground squirrels, they have large cheek pouches, sometimes extending as far back as their shoulders, in which they can store food They collect and store nuts and seeds through the summer and fall When the weather starts to get cool, all the chipmunks in a region suddenly disappear into their lodges, where they begin hibernation On warm winter days one can often see chipmunk pawprints in the snow, as they will sometimes wake up and leave their lodges for brief periods when the temperature rises Mating season for Eastern chipmunks is mid-March to early April The gestation period is 31 days, after which a litter of three to six is born Baby chipmunks leave the lodge after one month and are mature by July The chipmunk most likely got its name from the noise it makes, which sounds like a loud “cheep.” You can occasionally see a chipmunk hanging upside down from a tree branch “cheeping” its call Matt Hutchinson was the production editor and copyeditor for C++ in a Nutshell Sarah Sherman and Claire Cloutier provided quality control Julie Hawks wrote the index Derek Di Matteo and Mary Brady provided production assistance Ellie Volckhausen designed the cover of this book, based on a series design by Edie Freedman The cover image is a 19th-century engraving from the Dover Pictorial Archive Emma Colby produced the cover layout with QuarkXPress 4.1 using Adobe’s ITC Garamond font David Futato designed the interior layout This book was converted by Joe Wizda and Andrew Savikas to FrameMaker 5.5.6 with a format conversion tool created by Erik Ray, Jason McIntosh, Neil Walls, and Mike Sierra that uses Perl and XML technologies The text font is Linotype Birka; the heading font is Adobe Myriad Condensed; and the code font is LucasFont’s TheSans Mono Condensed The illustrations that appear in the book were produced by Robert Romano and Jessamyn Read using Macromedia FreeHand and Adobe Photoshop The tip and warning icons were drawn by Christopher Bing This colophon was written by Clairemarie Fisher O’Leary ... have internal linkage, as const declarations that are not also extern Data members of anonymous unions have internal linkage Names in an unnamed namespace have internal linkage External linkage... newline) A wide string literal is prefaced with L (always uppercase) In a wide string literal, a single universal character always maps to a single wide character In a narrow string literal,... enclosing blocks are searched Finally, the namespaces that contain the class declaration are searched That is, the namespace that immediately contains the class is searched first; if that namespace

Ngày đăng: 12/10/2020, 17:39

Mục lục

  • Preface

    • Structure of This Book

    • Conventions Used in This Book

    • Name Lookup

      • Qualified Name Lookup

      • Object Declarations

        • Specifiers

          • Storage class specifiers

          • const and volatile qualifiers

          • How to Use Namespaces

          • Constant Expressions

            • Integral Constant Expressions

            • Bitwise Exclusive Or Expressions

            • Bitwise Inclusive Or Expressions

            • Variable Number of Arguments

            • Function Definitions

              • Declarations and Definitions

              • Function Types and Signatures

              • Function Bodies and try Blocks

              • Function Overloading

                • Declaring Overloaded Functions

                • Candidate Functions

                  • Qualified member function call

                  • Reference bound to conversion

                  • Addresses of Overloaded Functions

                  • Best Overloaded Function

                    • Pruning the candidate list

                    • Choosing the best function

                    • operator new and operator delete

Tài liệu cùng người dùng

Tài liệu liên quan