C++ in a nutshell

810 4.8K 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

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 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] www.it-ebooks.info v This is the Title of the Book, eMatter Edition Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved. Chapter 1 Table of 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 www.it-ebooks.info vi | Table of Contents 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 www.it-ebooks.info Table of Contents | vii 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 www.it-ebooks.info viii | Table of Contents 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 www.it-ebooks.info [...]... String literals String literals are enclosed in double quotes A string contains characters that are similar to character literals: plain characters, escape sequences, and universal characters A string cannot cross a line boundary in the source file, but it can contain escaped line endings (backslash followed by newline) A wide string literal is prefaced with L (always uppercase) In a wide string literal,... string literal, a single universal character always maps to a single wide character In a narrow string literal, the implementation determines whether a universal character maps to one or multiple characters (called a multibyte character) See Chapter 8 for more information on multibyte characters Two adjacent string literals (possibly separated by whitespace, including new lines) are concatenated at compile... separate definitions and declarations, states when definitions are required, and outlines any other rules pertaining to declarations and definitions Ambiguity Some language constructs can look like a declaration or an expression Such ambiguities are always resolved in favor of declarations A related rule is that a declaration that is a type specifier followed by a name and empty parentheses is a declaration... and its base classes are searched If the class is local to a function, the block that contains the class is searched, then 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 is nested within another namespace, the outer namespace is searched next, and so on... example, suppose you want to construct a vector of integers by reading a series of numbers from the standard input Your first attempt might be to use an istream_iterator: using namespace std; vector data(istream_iterator(cin), istream_iterator( )); This declaration actually declares a function named data, which takes two parameters The first is named cin and has type istream_iterator... time into a single string This is often a convenient way to break a long string across multiple lines Do not try to combine a narrow string with a wide string in this way After concatenating adjacent strings, the null character ('\0' or L'\0') is automatically appended after the last character in the string literal Here are some examples of string literals Note that the first three form identical strings... information about preprocessing, see Chapter 11 5 Convert character and string literals to the execution character set 6 Concatenate adjacent string literals Narrow string literals are concatenated with narrow string literals Wide string literals are concatenated with wide string literals Mixing narrow and wide string literals results in an error 7 Perform the main compilation 8 Combine compiled files For each... literal value is implementation-defined Note that a character might have different encodings in different locales Consult your compiler’s documentation to learn which encoding it uses for character literals A narrow character literal with a single character has type char With more than one character, the type is int (e.g., 'abc') The type of a wide character literal is always wchar_t In C, a character... of a declaration The difference between a declaration and a definition is that a declaration tells you an entity’s name and the external view of the entity, such as an object’s type or a function’s parameters, and a definition provides the internal workings of the entity: the storage and initial value of an object, a function body, and so on In a single source file, there can be at most one definition... literal always has type int C++ changed the type of character literals to support overloading, especially for I/O (e.g., cout . a backslash character is followed immediately by a newline character, delete the backslash and the newline. The backslash/newline combination must not fall in the middle of a universal character. more information about prepro- cessing, see Chapter 11. 5. Convert character and string literals to the execution character set. 6. Concatenate adjacent string literals. Narrow string literals are. members, virtual functions, inheritance, accessibility, and multiple inheritance. Chapter 7, Templates, describes class and function template declarations, defini- tions, instantiations, specializations, and

Ngày đăng: 24/04/2014, 14:53

Từ khóa liên quan

Mục lục

  • Table of Contents

  • Preface

    • Structure of This Book

    • About the Examples

    • Conventions Used in This Book

    • For More Information

    • Comments and Questions

    • Acknowledgments

    • Language Basics

      • Compilation Steps

      • Tokens

        • Identifiers

        • Keywords

        • Literals

          • Integer literals

          • Floating-point literals

          • Boolean literals

          • Character literals

          • String literals

          • Symbols

          • Comments

          • Character Sets

          • Alternative Tokens

          • Trigraphs

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

  • Đang cập nhật ...

Tài liệu liên quan