www.it-ebooks.info
www.it-ebooks.info
C++ IN A NUTSHELL
www.it-ebooks.info
www.it-ebooks.info
C++ IN A NUTSHELL
Ray Lischner
Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol • Taipei • Tokyo
www.it-ebooks.info
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
[email protected].
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]
www.it-ebooks.info
Chapter 1
Table of Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix 1. Language Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Compilation Steps Tokens Comments Character Sets Alternative Tokens Trigraphs
1 2 8 8 9 10
2. Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Declarations and Definitions Scope Name Lookup Linkage Type Declarations Object Declarations Namespaces
12 14 16 22 24 29 42
3. 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 www.it-ebooks.info Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
4. Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Expression Statements Declarations Compound Statements Selections Loops Control Statements Handling Exceptions
83 84 86 87 89 92 94
5. Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 Function Declarations Function Definitions Function Overloading Operator Overloading The main Function
98 106 109 124 130
6. Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 Class Definitions Data Members Member Functions Inheritance Access Specifiers Friends Nested Types
132 139 142 155 167 170 172
7. 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
8. 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 www.it-ebooks.info Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
211 215 215 217
Allocators Numerics
223 225
9. 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 www.it-ebooks.info Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
vii