ebook img

C++ in a Nutshell PDF

800 Pages·2003·2.201 MB·english
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview C++ in a Nutshell

++ C IN A NUTSHELL Ray Lischner C++ in a Nutshell by Ray Lischner Copyright © 2003 O’Reilly Media, Inc. Printed in the United States of America. Printing History: May 2003: First Edition. ISBN-10: 0-596-00298-X ISBN-13: 978-0-596-00298-5 [M] [5/07] Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .ix 1. Language Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Compilation Steps 1 Tokens 2 Comments 8 Character Sets 8 Alternative Tokens 9 Trigraphs 10 2. Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Declarations and Definitions 12 Scope 14 Name Lookup 16 Linkage 22 Type Declarations 24 Object Declarations 29 Namespaces 42 3. Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Lvalues and Rvalues 50 Type Conversions 52 Constant Expressions 56 Expression Evaluation 57 Expression Rules 59 v This is the Title of the Book, eMatter Edition Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved. 4. Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Expression Statements 83 Declarations 84 Compound Statements 86 Selections 87 Loops 89 Control Statements 92 Handling Exceptions 94 5. Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 Function Declarations 98 Function Definitions 106 Function Overloading 109 Operator Overloading 124 The main Function 130 6. Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 Class Definitions 132 Data Members 139 Member Functions 142 Inheritance 155 Access Specifiers 167 Friends 170 Nested Types 172 7. Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 Overview of Templates 175 Template Declarations 177 Function Templates 180 Class Templates 186 Specialization 191 Partial Specialization 194 Instantiation 195 Name Lookup 199 Tricks with Templates 205 Compiling Templates 208 8. Standard Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 Overview of the Standard Library 211 C Library Wrappers 215 Wide and Multibyte Characters 215 Traits and Policies 217 vi | Table of Contents This is the Title of the Book, eMatter Edition Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved. Allocators 223 Numerics 225 9. Input and Output. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 Introduction to I/O Streams 229 Text I/O 235 Binary I/O 237 Stream Buffers 237 Manipulators 241 Errors and Exceptions 243 10. Containers, Iterators, and Algorithms . . . . . . . . . . . . . . . . . . . . . . . . 246 Containers 246 Iterators 261 Algorithms 266 11. Preprocessor Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276 12. Language Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290 13. Library Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327 <algorithm> 328 <bitset> 369 <cassert> 375 <cctype> 376 <cerrno> 378 <cfloat> 380 <ciso646> 384 <climits> 384 <clocale> 386 <cmath> 390 <complex> 397 <csetjmp> 406 <csignal> 407 <cstdarg> 410 <cstddef> 412 <cstdio> 413 <cstdlib> 429 <cstring> 439 <ctime> 445 <cwchar> 450 <cwctype> 465 Table of Contents | vii This is the Title of the Book, eMatter Edition Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved. <deque> 470 <exception> 475 <fstream> 478 <functional> 487 <iomanip> 503 <ios> 504 <iosfwd> 523 <iostream> 525 <istream> 527 <iterator> 535 <limits> 553 <list> 558 <locale> 564 <map> 602 <memory> 613 <new> 623 <numeric> 627 <ostream> 629 <queue> 634 <set> 638 <sstream> 647 <stack> 655 <stdexcept> 657 <streambuf> 660 <string> 667 <strstream> 686 <typeinfo> 693 <utility> 695 <valarray> 698 <vector> 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. Preface C++inaNutshellisareferencetotheC++languageandlibrary.BeingaNutshell guide,itisnotacomprehensivemanual,butitiscompleteenoughtocoverevery- thingaworkingprofessionalneedstoknow.Nonetheless,C++issuchalargeand complex language that even this Nutshell guide is a large book. ThisbookcoverstheC++standard,theinternationalstandardpublishedasISO/IEC 14882:1998(E), Programming Languages—C++, plus Technical Corrigendum 1. ManyimplementationsofC++extendthelanguageandstandardlibrary.Exceptfor briefmentionsoflanguageandlibraryextensionsintheappendixes,thisbookcovers only the standard. The standard library is large—it includes strings, containers, common algorithms, and much more—but it omits much that is commonplace in computing today: concurrency, network protocols, database access, graphics, windows, and so on. See AppendixB for information about nonstandard libraries that provide additional functionality. This book is a reference. It is not a tutorial. Newcomers to C++ might find portions of this book difficult to understand. Although each section contains some advice on idioms and the proper use of certain language constructs, the main focus is on the reference material. Visit http://www.tempest-sw.com/cpp/ for links to sites and lists of books that are better suited for beginners. Structure of This Book Thisbookisdividedintotwointerleavedsectionsthatcoverthelanguageandthe library,andasectionofappendixes.Roughlyspeaking,thelanguageisthepartof C++thatdoesnotrequireanyadditional#includeheadersorfiles.Thelibraryis the part of C++ that is declared in the standard headers. Chapters 1–7, 11, and 12 cover the language. The first seven chapters form the main language reference, organized by topic. It is customary for a programming reference to contain a formal grammar, and this book does so in Chapter12, ix This is the Title of the Book, eMatter Edition Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved. which is organized alphabetically by keyword (with some additional entries for majorsyntacticcategories,suchasexpressions).Chapter11isareferenceforthe preprocessor. Chapter13 is the library reference, organized alphabetically by header. Chapters 8–10 present an overview of the library and introduce the topics that span indi- vidual headers. Sometimes,informationisduplicated,especiallyinChapter12.Mygoalhasbeen topresentinformationwhenyouneedit,whereyouneedit.Itriedtobalancethe need for a single, clear, complete description of each language feature with the desire to reduce the number of cross references you must chase before you can understand that language feature. Here are more detailed descriptions of each chapter. Chapter1,LanguageBasics,describesthebasicrulesfortheC++language:char- acter sets, tokens, literals, and so on. Chapter2, Declarations, describes how objects, types, and namespaces are declared and how names are looked up. Chapter3,Expressions, describes operators, precedence, and type casts. Chapter4,Statements, describes all the C++ statements. Chapter5, Functions, describes function declarations and definitions, overload resolution, argument passing, and related topics. Chapter6, Classes, describes classes (and unions and structures), members, virtual functions, inheritance, accessibility, and multiple inheritance. Chapter7, Templates, describes class and function template declarations, defini- tions, instantiations, specializations, and how templates are used. Chapter8, Standard Library, introduces the standard library and discusses some overarching topics, such as traits and allocators. Chapter9, Input and Output, introduces the I/O portion of the standard library. Topics include formatted and unformatted I/O, stream buffers, and manipulators. Chapter10, Containers, Iterators, and Algorithms, introduces the suite of container class templates, their iterators, and generic algorithms. This is the portion of the library that has traditionally been called the Standard Template Library (STL). Chapter11, Preprocessor Reference, is an alphabetical reference for the prepro- cessor, which is part of the language, but with a distinct set of syntactic and semantic rules. Chapter12,LanguageReference,isanalphabeticalreferenceforthelanguageand grammar. Backus-Naur Form (BNF) syntax descriptions are given for each keywordandotherlanguageelements,withpointerstothefirstsevenchaptersfor the main reference material. Chapter13,LibraryReference,isareferencefortheentirestandardlibrary,orga- nized alphabetically by header, and alphabetically by name within each header section. x | Preface This is the Title of the Book, eMatter Edition Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved. AppendixA,CompilerExtensions,describeswaysthatsomecompilersextendthe language:tosatisfycustomerneed,tomeetplatform-specificrequirements,andso on. AppendixB,Projects,describesafewinteresting,opensourceC++projects.You can find information about additional projects on this book’s web site (http:// www.tempest-sw.com/cpp/). The Glossary defines some words and phrases used throughout this book and in the C++ community. About the Examples Whenever possible, the examples in this book are complete, compilable programs. You can tell which examples fall into this category because they start with#includedirectivesandcontainamain()function.Youcandownloadthese examplesastextfilesfromthebook’swebsiteathttp://www.tempest-sw.com/cpp/ or from O’Reilly’s catalog page for this book: http://www.oreilly.com/catalog/ cplsian/. Most examples are shortened to eliminate excess code that might interfere with theclarityoftheexample.Inparticular,theseexamplesarefragmentsthatlacka main function. Sometimes, an ellipsis indicates missing code, such as a function body. In other cases, the omissions are clear from the context. Most abbreviated examples have complete and compilable versions available for download. Alloftheexampleshavebeencheckedwithseveraldifferentcompilers,including Comeau Computing’s compiler with the Dinkumware standard library (widely acknowledged as the most complete and correct implementations of the C++ standard). Not all compilers can compile all the examples due to limitations and bugs in the compilers and libraries. For best results, try to work with the latest version of your compiler. Recent releases of several major compilers have made dramaticprogresstowardconformancewiththestandard.Whenpossible,Ihave tried to alter the example files to work around the bugs without interfering with the intent of the example. I have checked all the examples with the following compilers: Linux • Borland Kylix 3.0 • Comeau 4.3.0.1 • GNU 3.2 • Intel 7.0 Microsoft Windows • Borland C++ Builder 6.4 • Metrowerks CodeWarrior 8.3 • Microsoft Visual Studio.NET 7.0 Preface | xi This is the Title of the Book, eMatter Edition Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved. Conventions Used in This Book This book uses the following conventions: Constant Width Used for identifiers and symbols, including all keywords. In the language reference,constantwidthshowssyntaxelementsthatmustbeusedexactlyas shown.Forexample,theifkeyword,parentheses,andelsekeywordmustbe used exactly as follows: if ( condition ) statement else statement Afunctionnamethatisfollowedbyparenthesesreferstoafunctioncall,typi- cally to obtain the function result. The function name without the parentheses refers to the function in more general terms. For example: Theemptyfunctionreturnstrueifthecontainerisempty,e.g.,size()== 0. Constant Width Italic Used in the language reference chapters for syntax elements that must be replaced by your code. In the previous example, you must supply the condition and the twostatements. Constant Width Bold Usedinexamplestohighlightkeylines,andincomplexdeclarationstohigh- light the name being declared. In some C++ declarations, especially for templates, the name gets buried in the middle of the declaration and can be hard to spot. Italic Usedinthelanguagereferencefornonterminalsyntaxelements.Italicisalso used for filenames, URLs, emphasis, and for the first use of a technical term. ... Indicatesstatementsanddeclarationsthathavebeenremovedforthesakeof brevity and clarity. An ellipsis is also a symbol in C++, but context and comments make it clear when an ellipsis is a language element and when it represents omitted code. [first, last) Indicatesarangeofvaluesfromfirsttolast,includingfirstandexcluding last. This icon indicates a tip, suggestion, or general note. This icon indicates a warning or caution. xii | Preface This is the Title of the Book, eMatter Edition Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.