Statistics
42
Views
0
Downloads
0
Donations
Support
Share
Uploader

高宏飞

Shared on 2025-12-09

AuthorIvor Horton, Peter Van Weert

Overview Begin to master the C++23 standard through step-by-step examples Includes modular programming, parallelism, and more Also covers arrays, lambdas, operator overloading, inheritance, virtual functions, and polymorphism About this Book Begin your programming journey with C++ , starting with the basics and progressing through step-by-step examples that will help you become a proficient C++ programmer. This book includes new features from the C++23 standard. All you need are Beginning C++23 and any recent C++ compiler and you'll soon be writing real C++ programs. There is no assumption of prior programming knowledge. All language concepts that are explained in the book are illustrated with working program examples, and all chapters include exercises for you to test and practice your knowledge. Free source code downloads are provided for all examples from the text and solutions to the exercises. This latest edition has been fully updated to the latest version of the language, C++23, and to all conventions and best practices of modern C++. This book also introduces elements of the C++ Standard Library that provide essential support for C++23. After completing this book, you will have the knowledge and skills needed to build your first C++ applications. What You Will Learn Begin programming with the C++23 standard Carry out modular programming in C++ Work with arrays and loops, pointers and references, strings, and more Write your own functions, types, and operators Discover the essentials of object-oriented programming Use overloading, inheritance, virtual functions, and polymorphism Write generic function and class templates, and make them safer using concepts Learn the ins and outs of containers, algorithms, and ranges Use auto type declarations, exceptions, move semantics, lambda expressions, and much more Who This Book Is For Programmers new to C++ and those who may be looking for a refresh primer o

Tags
No tags
ISBN: 1484293428
Publisher: Apress
Publish Year: 2023
Language: 英文
Pages: 938
File Format: PDF
File Size: 9.3 MB
Support Statistics
¥.00 · 0times
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Beginning C++23 From Beginner to Pro — Seventh Edition — Ivor Horton Peter Van Weert
Beginning C++23 From Beginner to Pro Seventh Edition Ivor Horton Peter Van Weert
Beginning C++23: From Beginner to Pro ISBN-13 (pbk): 978-1-4842-9342-3 ISBN-13 (electronic): 978-1-4842-9343-0 https://doi.org/10.1007/978-1-4842-9343-0 Copyright © 2023 by Ivor Horton and Peter Van Weert This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director, Apress Media LLC: Welmoed Spahr Acquisitions Editor: Steve Anglin Development Editor: James Markham Editorial Assistant: Gryffin Winkler Copyeditor: Kim Burton Cover designed by eStudioCalamar Cover image by @Harryarts on Freepik (www.freepik.com) Distributed to the book trade worldwide by Springer Science+Business Media New York, 1 New York Plaza, Suite 4600, New York, NY 10004-1562, USA. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@ springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail booktranslations@springernature.com; for reprint, paperback, or audio rights, please e-mail bookpermissions@springernature.com. Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales web page at http://www.apress.com/bulk-sales. Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub (github.com/apress). For more detailed information, please visit https://www.apress.com/gp/ services/source-code. Paper in this product is recyclable Ivor Horton Stratford-upon-Avon, Warwickshire, UK Peter Van Weert Kessel-Lo, Belgium
For my wonderful family. For all your love and support. —Peter Van Weert In memory of my wife, Eve —Ivor Horton
v Table of Contents ■About the Authorsxxv ■About the Technical Reviewer xxvii ■Introduction xxix ■Chapter 1: Basic Ideas  1 Modern C++ ��������������������������������������������������������������������������������������������������������������������� 1 Standard Libraries ������������������������������������������������������������������������������������������������������������ 2 C++ Program Concepts ���������������������������������������������������������������������������������������������������� 3 Source Files �������������������������������������������������������������������������������������������������������������������������������������������� 3 Comments and Whitespace �������������������������������������������������������������������������������������������������������������������� 4 The Standard Library and Modules��������������������������������������������������������������������������������������������������������� 5 Functions ������������������������������������������������������������������������������������������������������������������������������������������������ 6 Statements ��������������������������������������������������������������������������������������������������������������������������������������������� 6 Text Output ��������������������������������������������������������������������������������������������������������������������������������������������� 7 return Statements����������������������������������������������������������������������������������������������������������������������������������� 9 Namespaces ������������������������������������������������������������������������������������������������������������������������������������������� 9 Identifiers and Keywords ���������������������������������������������������������������������������������������������������������������������� 10 Streams �������������������������������������������������������������������������������������������������������������������������� 11 Classes and Objects ������������������������������������������������������������������������������������������������������� 12 Templates ����������������������������������������������������������������������������������������������������������������������� 12 Code Appearance and Programming Style ��������������������������������������������������������������������� 13 Creating an Executable �������������������������������������������������������������������������������������������������� 13 Procedural and Object-Oriented Programming �������������������������������������������������������������� 15
vi Representing Numbers ��������������������������������������������������������������������������������������������������� 16 Binary Numbers ������������������������������������������������������������������������������������������������������������������������������������ 16 Hexadecimal Numbers ������������������������������������������������������������������������������������������������������������������������� 18 Negative Binary Numbers ��������������������������������������������������������������������������������������������������������������������� 19 Octal Values ������������������������������������������������������������������������������������������������������������������������������������������ 20 Big-Endian and Little-Endian Systems ������������������������������������������������������������������������������������������������� 21 Floating-Point Numbers ������������������������������������������������������������������������������������������������������������������������ 22 Representing Characters ������������������������������������������������������������������������������������������������ 24 ASCII Codes ������������������������������������������������������������������������������������������������������������������������������������������ 24 UCS and Unicode ���������������������������������������������������������������������������������������������������������������������������������� 24 Summary ������������������������������������������������������������������������������������������������������������������������ 25 ■Chapter 2: Introducing Fundamental Types of Data  27 Variables, Data, and Data Types ������������������������������������������������������������������������������������� 27 Defining Integer Variables��������������������������������������������������������������������������������������������������������������������� 28 Zero Initialization ���������������������������������������������������������������������������������������������������������������������������������� 32 Defining Variables with Fixed Values ���������������������������������������������������������������������������������������������������� 32 Integer Literals ��������������������������������������������������������������������������������������������������������������� 32 Decimal Integer Literals������������������������������������������������������������������������������������������������������������������������ 32 Hexadecimal Literals ���������������������������������������������������������������������������������������������������������������������������� 34 Octal Literals ����������������������������������������������������������������������������������������������������������������������������������������� 34 Binary Literals��������������������������������������������������������������������������������������������������������������������������������������� 35 Calculations with Integers ���������������������������������������������������������������������������������������������� 35 Compound Arithmetic Expressions ������������������������������������������������������������������������������������������������������� 37 Assignment Operations �������������������������������������������������������������������������������������������������� 37 The op= Assignment Operators ������������������������������������������������������������������������������������������������������������ 40 Incrementing and Decrementing Integers ���������������������������������������������������������������������� 42 Postfix Increment and Decrement Operations �������������������������������������������������������������������������������������� 42 Floating-Point Variables ������������������������������������������������������������������������������������������������� 44 Standard Floating-Point Data Types ����������������������������������������������������������������������������������������������������� 44 Extended Floating-Point Data Types ����������������������������������������������������������������������������������������������������� 45 ■ Table of ConTenTs
vii Floating-Point Literals ���������������������������������������������������������������������������������������������������� 47 Finding the Limits ����������������������������������������������������������������������������������������������������������� 48 Finding Other Properties of Fundamental Types ����������������������������������������������������������������������������������� 49 The sizeof Operator �������������������������������������������������������������������������������������������������������� 49 Floating-Point Calculations �������������������������������������������������������������������������������������������� 50 Mathematical Constants ����������������������������������������������������������������������������������������������������������������������� 51 Mathematical Functions ����������������������������������������������������������������������������������������������������������������������� 51 Invalid Floating-Point Results ��������������������������������������������������������������������������������������������������������������� 55 Pitfalls ��������������������������������������������������������������������������������������������������������������������������������������������������� 57 Mixed Expressions and Type Conversion ������������������������������������������������������������������������ 57 Explicit Type Conversion ������������������������������������������������������������������������������������������������� 59 Old-Style Casts ������������������������������������������������������������������������������������������������������������������������������������� 62 The auto Keyword ���������������������������������������������������������������������������������������������������������� 62 Working with Character Variables ���������������������������������������������������������������������������������� 63 Working with Unicode Characters �������������������������������������������������������������������������������������������������������� 64 Unicode Literals and Source File Encodings ����������������������������������������������������������������������������������������� 65 Escape Sequences ��������������������������������������������������������������������������������������������������������� 66 Escaping Unicode Characters ��������������������������������������������������������������������������������������������������������������� 67 Formatting ���������������������������������������������������������������������������������������������������������������������� 69 Modern Formatting ������������������������������������������������������������������������������������������������������������������������������� 69 Formatting Stream Output �������������������������������������������������������������������������������������������������������������������� 74 Summary ������������������������������������������������������������������������������������������������������������������������ 75 ■Chapter 3: Working with Fundamental Data Types  79 Operator Precedence and Associativity �������������������������������������������������������������������������� 79 Bitwise Operators ����������������������������������������������������������������������������������������������������������� 81 The Bitwise Shift Operators ������������������������������������������������������������������������������������������������������������������ 82 Logical Operations on Bit Patterns ������������������������������������������������������������������������������������������������������� 85 The Lifetime of a Variable ����������������������������������������������������������������������������������������������� 92 ■ Table of ConTenTs
viii Global Variables �������������������������������������������������������������������������������������������������������������� 93 Enumerated Data Types �������������������������������������������������������������������������������������������������� 97 Aliases for Data Types �������������������������������������������������������������������������������������������������� 101 Summary ���������������������������������������������������������������������������������������������������������������������� 102 ■Chapter 4: Making Decisions  105 Comparing Data Values ������������������������������������������������������������������������������������������������ 105 Applying the Comparison Operators ��������������������������������������������������������������������������������������������������� 106 Comparing Floating-Point Values ������������������������������������������������������������������������������������������������������� 108 The Spaceship Operator ��������������������������������������������������������������������������������������������������������������������� 109 The if Statement ����������������������������������������������������������������������������������������������������������� 112 Nested if Statements �������������������������������������������������������������������������������������������������������������������������� 116 Character Classification and Conversion �������������������������������������������������������������������������������������������� 117 The if-else Statement ��������������������������������������������������������������������������������������������������� 119 Nested if-else Statements ������������������������������������������������������������������������������������������������������������������ 121 Understanding Nested ifs ������������������������������������������������������������������������������������������������������������������� 122 Logical Operators ��������������������������������������������������������������������������������������������������������� 124 Logical AND ���������������������������������������������������������������������������������������������������������������������������������������� 124 Logical OR ������������������������������������������������������������������������������������������������������������������������������������������� 125 Logical Negation ��������������������������������������������������������������������������������������������������������������������������������� 125 Combining Logical Operators ������������������������������������������������������������������������������������������������������������� 126 Logical Operators on Integer Operands ���������������������������������������������������������������������������������������������� 128 Logical Operators vs� Bitwise Operators �������������������������������������������������������������������������������������������� 128 The Conditional Operator ���������������������������������������������������������������������������������������������� 130 The switch Statement �������������������������������������������������������������������������������������������������� 132 Fallthrough ����������������������������������������������������������������������������������������������������������������������������������������� 137 Switching on Enumeration Values ������������������������������������������������������������������������������������������������������ 139 Statement Blocks and Variable Scope �������������������������������������������������������������������������� 140 Initialization Statements ��������������������������������������������������������������������������������������������������������������������� 142 Summary ���������������������������������������������������������������������������������������������������������������������� 143 ■ Table of ConTenTs
ix ■Chapter 5: Arrays and Loops  147 Arrays ��������������������������������������������������������������������������������������������������������������������������� 147 Using an Array ������������������������������������������������������������������������������������������������������������������������������������ 147 Understanding Loops ��������������������������������������������������������������������������������������������������� 149 The for Loop ����������������������������������������������������������������������������������������������������������������� 150 Avoiding Magic Numbers ��������������������������������������������������������������������������������������������� 152 Defining the Array Size with the Braced Initializer ������������������������������������������������������� 154 Determining the Size of an Array ��������������������������������������������������������������������������������� 154 Controlling a for Loop with Floating-Point Values �������������������������������������������������������� 156 More Complex for Loop Control Expressions ���������������������������������������������������������������� 158 The Comma Operator �������������������������������������������������������������������������������������������������������������������������� 159 The Range-Based for Loop ������������������������������������������������������������������������������������������� 160 The while Loop ������������������������������������������������������������������������������������������������������������� 161 The do-while Loop �������������������������������������������������������������������������������������������������������� 164 Nested Loops ���������������������������������������������������������������������������������������������������������������� 166 Skipping Loop Iterations ����������������������������������������������������������������������������������������������� 168 Breaking Out of a Loop ������������������������������������������������������������������������������������������������� 170 Indefinite Loops ���������������������������������������������������������������������������������������������������������������������������������� 171 Controlling a for Loop with Unsigned Integers ������������������������������������������������������������� 174 Arrays of Characters ����������������������������������������������������������������������������������������������������� 176 Multidimensional Arrays ����������������������������������������������������������������������������������������������� 179 Initializing Multidimensional Arrays ���������������������������������������������������������������������������������������������������� 181 Multidimensional Character Arrays ���������������������������������������������������������������������������������������������������� 184 Allocating an Array at Runtime ������������������������������������������������������������������������������������� 186 Alternatives to Using an Array �������������������������������������������������������������������������������������� 188 Using array<T,N> Containers ������������������������������������������������������������������������������������������������������������� 188 Using std::vector<T> Containers �������������������������������������������������������������������������������������������������������� 194 Formatting Ranges ������������������������������������������������������������������������������������������������������� 199 Summary ���������������������������������������������������������������������������������������������������������������������� 201 ■ Table of ConTenTs
x ■Chapter 6: Pointers and References  205 What Is a Pointer? �������������������������������������������������������������������������������������������������������� 205 The Address-Of Operator ���������������������������������������������������������������������������������������������� 208 The Indirection Operator ����������������������������������������������������������������������������������������������� 210 Why Use Pointers? ������������������������������������������������������������������������������������������������������� 211 Pointers to Type char ���������������������������������������������������������������������������������������������������� 212 Arrays of Pointers ������������������������������������������������������������������������������������������������������������������������������� 214 Constant Pointers and Pointers to Constants ��������������������������������������������������������������� 216 Pointers and Arrays ������������������������������������������������������������������������������������������������������ 219 Pointer Arithmetic ������������������������������������������������������������������������������������������������������������������������������� 219 Using Pointer Notation with an Array Name ��������������������������������������������������������������������������������������� 222 Dynamic Memory Allocation ����������������������������������������������������������������������������������������� 225 The Stack and the Free Store ������������������������������������������������������������������������������������������������������������� 225 Using the new and delete Operators �������������������������������������������������������������������������������������������������� 226 Dynamic Allocation of Arrays �������������������������������������������������������������������������������������������������������������� 228 Member Selection Through a Pointer ��������������������������������������������������������������������������� 232 Hazards of Dynamic Memory Allocation ����������������������������������������������������������������������� 232 Dangling Pointers and Multiple Deallocations ������������������������������������������������������������������������������������ 233 Allocation/Deallocation Mismatch ������������������������������������������������������������������������������������������������������ 233 Memory Leaks ������������������������������������������������������������������������������������������������������������������������������������ 233 Fragmentation of the Free Store ��������������������������������������������������������������������������������������������������������� 234 Golden Rule of Dynamic Memory Allocation ���������������������������������������������������������������� 235 Raw Pointers and Smart Pointers �������������������������������������������������������������������������������� 235 Using unique_ptr<T> Pointers ����������������������������������������������������������������������������������������������������������� 237 Using shared_ptr<T> Pointers ����������������������������������������������������������������������������������������������������������� 240 Understanding References ������������������������������������������������������������������������������������������� 244 Defining References ��������������������������������������������������������������������������������������������������������������������������� 245 Using a Reference Variable in a Range-Based for Loop ��������������������������������������������������������������������� 246 Summary ���������������������������������������������������������������������������������������������������������������������� 247 ■ Table of ConTenTs
xi ■Chapter 7: Working with Strings  251 A Better Class of String ������������������������������������������������������������������������������������������������ 251 Defining string Objects ����������������������������������������������������������������������������������������������������������������������� 252 Copying and Marking Substrings �������������������������������������������������������������������������������������������������������� 256 Formatting and Reading String Objects ���������������������������������������������������������������������������������������������� 257 Operations with String Objects ����������������������������������������������������������������������������������������������������������� 258 Accessing Characters in a String�������������������������������������������������������������������������������������������������������� 263 Accessing Substrings ������������������������������������������������������������������������������������������������������������������������� 265 Comparing Strings ������������������������������������������������������������������������������������������������������������������������������ 265 Searching Strings ������������������������������������������������������������������������������������������������������������������������������� 273 Modifying a String ������������������������������������������������������������������������������������������������������������������������������ 280 std::string vs� std::vector<char> �������������������������������������������������������������������������������������������������������� 285 Converting Strings into Numbers ��������������������������������������������������������������������������������� 286 Strings of International Characters ������������������������������������������������������������������������������ 286 Strings of wchar_t Characters ������������������������������������������������������������������������������������������������������������ 287 Objects That Contain Unicode Strings ������������������������������������������������������������������������������������������������ 288 Raw String Literals ������������������������������������������������������������������������������������������������������� 289 Summary ���������������������������������������������������������������������������������������������������������������������� 290 ■Chapter 8: Defining Functions  293 Segmenting Your Programs ������������������������������������������������������������������������������������������ 293 Functions in Classes ��������������������������������������������������������������������������������������������������������������������������� 294 Characteristics of a Function �������������������������������������������������������������������������������������������������������������� 294 Defining Functions ������������������������������������������������������������������������������������������������������� 294 The Function Body ������������������������������������������������������������������������������������������������������������������������������ 297 Return Values�������������������������������������������������������������������������������������������������������������������������������������� 298 Function Declarations ������������������������������������������������������������������������������������������������������������������������� 299 Passing Arguments to a Function ��������������������������������������������������������������������������������� 300 Pass-by-Value ������������������������������������������������������������������������������������������������������������������������������������� 301 Pass-by-Reference ����������������������������������������������������������������������������������������������������������������������������� 309 ■ Table of ConTenTs
xii Default Argument Values ���������������������������������������������������������������������������������������������� 316 Multiple Default Argument Values ������������������������������������������������������������������������������������������������������ 317 Arguments to main() ����������������������������������������������������������������������������������������������������� 319 Returning Values from a Function �������������������������������������������������������������������������������� 320 Returning a Pointer ����������������������������������������������������������������������������������������������������������������������������� 320 Returning a Reference ������������������������������������������������������������������������������������������������������������������������ 324 Returning vs� Output Parameters ������������������������������������������������������������������������������������������������������� 325 Return Type Deduction ������������������������������������������������������������������������������������������������������������������������ 326 Static Local Variables ��������������������������������������������������������������������������������������������������� 328 Function Overloading ��������������������������������������������������������������������������������������������������� 329 Overloading and Pointer Parameters �������������������������������������������������������������������������������������������������� 331 Overloading and Reference Parameters ��������������������������������������������������������������������������������������������� 332 Overloading and const Parameters ���������������������������������������������������������������������������������������������������� 333 Overloading and Default Argument Values ����������������������������������������������������������������������������������������� 335 Recursion ��������������������������������������������������������������������������������������������������������������������� 336 Basic Examples of Recursion ������������������������������������������������������������������������������������������������������������� 337 Recursive Algorithms �������������������������������������������������������������������������������������������������������������������������� 338 Constant Expressions ��������������������������������������������������������������������������������������������������� 345 constexpr Variables ���������������������������������������������������������������������������������������������������������������������������� 347 constexpr Functions ��������������������������������������������������������������������������������������������������������������������������� 348 Limitations to Constant Expressions �������������������������������������������������������������������������������������������������� 349 Summary ���������������������������������������������������������������������������������������������������������������������� 353 ■Chapter 9: Vocabulary Types  357 Working with Optional Values ��������������������������������������������������������������������������������������� 357 std::optional ���������������������������������������������������������������������������������������������������������������������������������������� 359 std::expected �������������������������������������������������������������������������������������������������������������������������������������� 361 String Views: The New Reference-to-const-string ������������������������������������������������������� 362 Using String View Function Parameters ��������������������������������������������������������������������������������������������� 364 A Proper Motivation ���������������������������������������������������������������������������������������������������������������������������� 365 ■ Table of ConTenTs
xiii Spans: The New Reference-to-vector or -array ����������������������������������������������������������� 365 Writing Through a Span ���������������������������������������������������������������������������������������������������������������������� 367 Spans of const Elements �������������������������������������������������������������������������������������������������������������������� 367 Fixed-Size Spans �������������������������������������������������������������������������������������������������������������������������������� 368 Summary ���������������������������������������������������������������������������������������������������������������������� 369 ■Chapter 10: Function Templates  371 Function Templates ������������������������������������������������������������������������������������������������������ 371 Creating Instances of a Function Template ������������������������������������������������������������������ 372 Template Type Parameters ������������������������������������������������������������������������������������������� 374 Explicit Template Arguments ���������������������������������������������������������������������������������������� 374 Function Template Specialization ��������������������������������������������������������������������������������� 375 Function Templates and Overloading ��������������������������������������������������������������������������� 376 Function Templates with Multiple Parameters ������������������������������������������������������������� 378 Return Type Deduction in Templates ���������������������������������������������������������������������������� 379 decltype(auto) ������������������������������������������������������������������������������������������������������������������������������������� 380 constexpr If ������������������������������������������������������������������������������������������������������������������ 381 Default Values for Template Parameters ���������������������������������������������������������������������� 382 Non-Type Template Parameters ����������������������������������������������������������������������������������� 383 Templates for Functions with Fixed-Size Array Arguments ���������������������������������������������������������������� 386 Abbreviated Function Templates ���������������������������������������������������������������������������������� 387 Limitations to Abbreviated Function Templates ���������������������������������������������������������������������������������� 388 Summary ���������������������������������������������������������������������������������������������������������������������� 388 ■Chapter 11: Modules and Namespaces  391 Modules ������������������������������������������������������������������������������������������������������������������������ 391 Your First Module �������������������������������������������������������������������������������������������������������������������������������� 392 Export Blocks �������������������������������������������������������������������������������������������������������������������������������������� 395 Separating Interface from Implementation����������������������������������������������������������������������������������������� 395 Reachability vs� Visibility �������������������������������������������������������������������������������������������������������������������� 401 Exporting Import Declarations ������������������������������������������������������������������������������������������������������������ 402 Managing Larger Modules ������������������������������������������������������������������������������������������������������������������ 403 ■ Table of ConTenTs
xiv Namespaces ����������������������������������������������������������������������������������������������������������������� 407 The Global Namespace ����������������������������������������������������������������������������������������������������������������������� 407 Defining a Namespace������������������������������������������������������������������������������������������������������������������������ 408 Nested Namespaces ��������������������������������������������������������������������������������������������������������������������������� 410 Namespaces and Modules ����������������������������������������������������������������������������������������������������������������� 411 Functions and Namespaces ��������������������������������������������������������������������������������������������������������������� 412 Using Directives and Declarations ������������������������������������������������������������������������������������������������������ 414 Namespace Aliases ���������������������������������������������������������������������������������������������������������������������������� 416 Summary ���������������������������������������������������������������������������������������������������������������������� 416 ■Chapter 12: Defining Your Own Data Types  419 Classes and Object-Oriented Programming ����������������������������������������������������������������� 419 Encapsulation ������������������������������������������������������������������������������������������������������������������������������������� 420 Inheritance ������������������������������������������������������������������������������������������������������������������������������������������ 424 Polymorphism ������������������������������������������������������������������������������������������������������������������������������������� 425 Terminology ������������������������������������������������������������������������������������������������������������������ 427 Defining a Class ����������������������������������������������������������������������������������������������������������� 427 Creating Objects of a Class ����������������������������������������������������������������������������������������������������������������� 429 Constructors ����������������������������������������������������������������������������������������������������������������� 431 Default Constructors ��������������������������������������������������������������������������������������������������������������������������� 431 Defining a Class Constructor �������������������������������������������������������������������������������������������������������������� 432 Using the default Keyword ����������������������������������������������������������������������������������������������������������������� 433 Defining Functions Outside the Class ������������������������������������������������������������������������������������������������� 434 Default Arguments for Constructor Parameters ��������������������������������������������������������������������������������� 435 Using a Member Initializer List ����������������������������������������������������������������������������������������������������������� 436 Using the explicit Keyword ����������������������������������������������������������������������������������������������������������������� 437 Delegating Constructors ��������������������������������������������������������������������������������������������������������������������� 439 The Copy Constructor ������������������������������������������������������������������������������������������������������������������������� 441 Defining Classes in Modules ���������������������������������������������������������������������������������������� 443 Accessing Private Class Members ������������������������������������������������������������������������������� 444 The this Pointer ������������������������������������������������������������������������������������������������������������ 446 Returning this from a Function ����������������������������������������������������������������������������������������������������������� 448 ■ Table of ConTenTs
xv const Objects and const Member Functions ���������������������������������������������������������������� 450 const Member Functions �������������������������������������������������������������������������������������������������������������������� 450 const Correctness ������������������������������������������������������������������������������������������������������������������������������� 452 Overloading on const �������������������������������������������������������������������������������������������������������������������������� 453 Casting Away const ���������������������������������������������������������������������������������������������������������������������������� 454 Using the mutable Keyword ���������������������������������������������������������������������������������������������������������������� 455 Friends ������������������������������������������������������������������������������������������������������������������������� 456 The Friend Functions of a Class ��������������������������������������������������������������������������������������������������������� 456 Friend Classes ������������������������������������������������������������������������������������������������������������������������������������ 458 Arrays of Class Objects ������������������������������������������������������������������������������������������������ 459 The Size of a Class Object �������������������������������������������������������������������������������������������� 461 Static Members of a Class ������������������������������������������������������������������������������������������� 461 Static Member Variables ��������������������������������������������������������������������������������������������������������������������� 461 Accessing Static Member Variables ��������������������������������������������������������������������������������������������������� 465 Static Constants ��������������������������������������������������������������������������������������������������������������������������������� 465 Static Member Variables of the Class Type Itself �������������������������������������������������������������������������������� 466 Static Member Functions�������������������������������������������������������������������������������������������������������������������� 467 Destructors ������������������������������������������������������������������������������������������������������������������� 468 constexpr Member Functions ��������������������������������������������������������������������������������������� 470 consteval Constructors ����������������������������������������������������������������������������������������������������������������������� 472 Using Pointers as Class Members �������������������������������������������������������������������������������� 473 The Truckload Example ����������������������������������������������������������������������������������������������������������������������� 474 Nested Classes ������������������������������������������������������������������������������������������������������������� 489 Nested Classes with Public Access ���������������������������������������������������������������������������������������������������� 490 A Better Mechanism for Traversing a Truckload: Iterators ������������������������������������������������������������������ 490 Summary ���������������������������������������������������������������������������������������������������������������������� 494 ■ Table of ConTenTs
xvi ■Chapter 13: Operator Overloading  499 Implementing Operators for a Class ����������������������������������������������������������������������������� 499 Operator Overloading �������������������������������������������������������������������������������������������������������������������������� 500 Implementing an Overloaded Operator ����������������������������������������������������������������������������������������������� 500 Nonmember Operator Functions �������������������������������������������������������������������������������������������������������� 503 Implementing Full Support for an Operator ���������������������������������������������������������������������������������������� 503 Operators That Can Be Overloaded ������������������������������������������������������������������������������ 505 Restrictions and Key Guideline ����������������������������������������������������������������������������������������������������������� 506 Operator Function Idioms ��������������������������������������������������������������������������������������������� 508 Supporting All Comparison Operators �������������������������������������������������������������������������� 509 Defaulting Comparison Operators ������������������������������������������������������������������������������������������������������ 512 Overloading the << Operator for Output Streams �������������������������������������������������������� 515 Overloading the Arithmetic Operators �������������������������������������������������������������������������� 517 Implementing One Operator in Terms of Another ������������������������������������������������������������������������������� 521 Member vs� Nonmember Functions ����������������������������������������������������������������������������� 522 Operator Functions and Implicit Conversions ������������������������������������������������������������������������������������� 524 Overloading Unary Operators ��������������������������������������������������������������������������������������� 525 Overloading the Increment and Decrement Operators ������������������������������������������������� 526 Overloading the Subscript Operator ����������������������������������������������������������������������������� 528 Modifying the Result of an Overloaded Subscript Operator ���������������������������������������������������������������� 531 Function Objects ���������������������������������������������������������������������������������������������������������� 533 Overloading Type Conversions ������������������������������������������������������������������������������������� 535 Potential Ambiguities with Conversions ��������������������������������������������������������������������������������������������� 536 Overloading the Assignment Operator �������������������������������������������������������������������������� 536 Implementing the Copy Assignment Operator ������������������������������������������������������������������������������������ 537 Copy Assignment vs� Copy Construction �������������������������������������������������������������������������������������������� 541 Deleting the Copy Assignment Operator ��������������������������������������������������������������������������������������������� 542 Assigning Different Types ������������������������������������������������������������������������������������������������������������������� 542 Summary ���������������������������������������������������������������������������������������������������������������������� 543 ■ Table of ConTenTs
xvii ■Chapter 14: Inheritance  547 Classes and Object-Oriented Programming ����������������������������������������������������������������� 547 Hierarchies ����������������������������������������������������������������������������������������������������������������������������������������� 548 Inheritance in Classes �������������������������������������������������������������������������������������������������� 549 Inheritance vs� Aggregation and Composition ������������������������������������������������������������������������������������ 550 Deriving Classes ��������������������������������������������������������������������������������������������������������������������������������� 550 Protected Members of a Class ������������������������������������������������������������������������������������� 553 The Access Level of Inherited Class Members ������������������������������������������������������������� 554 Access Specifiers and Class Hierarchies �������������������������������������������������������������������������������������������� 555 Choosing Access Specifiers in Class Hierarchies ������������������������������������������������������������������������������� 556 Changing the Access Specification of Inherited Members ����������������������������������������������������������������� 558 Constructors in a Derived Class ����������������������������������������������������������������������������������� 558 The Copy Constructor in a Derived Class �������������������������������������������������������������������������������������������� 562 The Default Constructor in a Derived Class ���������������������������������������������������������������������������������������� 563 Inheriting Constructors ����������������������������������������������������������������������������������������������������������������������� 564 Destructors Under Inheritance ������������������������������������������������������������������������������������� 565 Duplicate Member Variable Names ������������������������������������������������������������������������������ 567 Duplicate Member Function Names ����������������������������������������������������������������������������� 568 Multiple Inheritance ����������������������������������������������������������������������������������������������������� 569 Multiple Base Classes������������������������������������������������������������������������������������������������������������������������� 569 Inherited Member Ambiguity �������������������������������������������������������������������������������������������������������������� 570 Repeated Inheritance ������������������������������������������������������������������������������������������������������������������������� 574 Virtual Base Classes ��������������������������������������������������������������������������������������������������������������������������� 575 Converting Between Related Class Types �������������������������������������������������������������������� 576 Summary ���������������������������������������������������������������������������������������������������������������������� 577 ■ Table of ConTenTs
xviii ■Chapter 15: Polymorphism  579 Understanding Polymorphism �������������������������������������������������������������������������������������� 579 Using a Base Class Pointer ����������������������������������������������������������������������������������������������������������������� 579 Calling Inherited Functions ����������������������������������������������������������������������������������������������������������������� 581 Virtual Functions ���������������������������������������������������������������������������������������������������������� 585 Requirements for Virtual Function Operation ������������������������������������������������������������������������������������� 588 Using the override Specifier ��������������������������������������������������������������������������������������������������������������� 589 Using final ������������������������������������������������������������������������������������������������������������������������������������������� 590 Virtual Functions and Class Hierarchies ��������������������������������������������������������������������������������������������� 591 Access Specifiers and Virtual Functions ��������������������������������������������������������������������������������������������� 592 Default Argument Values in Virtual Functions ������������������������������������������������������������������������������������� 594 Using References to Call Virtual Functions ����������������������������������������������������������������������������������������� 595 Polymorphic Collections ����������������������������������������������������������������������������������������������� 596 Destroying Objects Through a Pointer �������������������������������������������������������������������������� 597 Virtual Destructors ������������������������������������������������������������������������������������������������������������������������������ 599 Converting Between Pointers to Class Objects ������������������������������������������������������������ 600 Dynamic Casts �������������������������������������������������������������������������������������������������������������� 602 Casting Pointers Dynamically ������������������������������������������������������������������������������������������������������������� 602 Dynamic Casts and const ������������������������������������������������������������������������������������������������������������������� 605 Converting References ����������������������������������������������������������������������������������������������������������������������� 606 Calling the Base Class Version of a Virtual Function ���������������������������������������������������� 606 Calling Virtual Functions from Constructors or Destructors ����������������������������������������� 607 How Polymorphic Function Calls Work ������������������������������������������������������������������������� 610 Determining the Dynamic Type ������������������������������������������������������������������������������������ 611 Pure Virtual Functions �������������������������������������������������������������������������������������������������� 615 Abstract Classes ��������������������������������������������������������������������������������������������������������������������������������� 616 An Abstract Box Class ������������������������������������������������������������������������������������������������������������������������� 617 Abstract Classes as Interfaces ����������������������������������������������������������������������������������������������������������� 619 Summary ���������������������������������������������������������������������������������������������������������������������� 622 ■ Table of ConTenTs
xix ■Chapter 16: Runtime Errors and Exceptions 625 Handling Errors ������������������������������������������������������������������������������������������������������������ 625 Understanding Exceptions �������������������������������������������������������������������������������������������� 626 Throwing an Exception ����������������������������������������������������������������������������������������������������������������������� 627 The Exception-Handling Process �������������������������������������������������������������������������������������������������������� 629 Code That Causes an Exception to Be Thrown ����������������������������������������������������������������������������������� 630 Nested try Blocks ������������������������������������������������������������������������������������������������������������������������������� 632 Class Objects as Exceptions ����������������������������������������������������������������������������������������� 633 Matching a Catch Handler to an Exception ����������������������������������������������������������������������������������������� 635 Catching Derived Class Exceptions with a Base Class Handler ���������������������������������������������������������� 638 Rethrowing Exceptions ������������������������������������������������������������������������������������������������� 640 Unhandled Exceptions �������������������������������������������������������������������������������������������������� 643 Catching All Exceptions ������������������������������������������������������������������������������������������������ 644 Functions That Don’t Throw Exceptions ����������������������������������������������������������������������� 647 The noexcept Specifier ����������������������������������������������������������������������������������������������������������������������� 647 Exceptions and Destructors ���������������������������������������������������������������������������������������������������������������� 648 Exceptions and Resource Leaks ����������������������������������������������������������������������������������� 649 Resource Acquisition Is Initialization �������������������������������������������������������������������������������������������������� 650 Standard RAII Classes for Dynamic Memory �������������������������������������������������������������������������������������� 652 Standard Library Exceptions ���������������������������������������������������������������������������������������� 654 The Exception Class Definitions ��������������������������������������������������������������������������������������������������������� 656 Using Standard Exceptions ����������������������������������������������������������������������������������������������������������������� 657 Stack Traces ����������������������������������������������������������������������������������������������������������������� 661 Summary ���������������������������������������������������������������������������������������������������������������������� 664 ■Chapter 17: Class Templates  669 Understanding Class Templates ����������������������������������������������������������������������������������� 669 Defining Class Templates ��������������������������������������������������������������������������������������������� 670 Template Type Parameters ����������������������������������������������������������������������������������������������������������������� 672 A Simple Class Template �������������������������������������������������������������������������������������������������������������������� 673 Defining Member Functions of a Class Template ��������������������������������������������������������� 674 ■ Table of ConTenTs
xx Constructor Templates ������������������������������������������������������������������������������������������������������������������������ 675 The Destructor Template��������������������������������������������������������������������������������������������������������������������� 676 Subscript Operator Templates ������������������������������������������������������������������������������������������������������������ 676 The Assignment Operator Template ���������������������������������������������������������������������������������������������������� 680 Class Template Instantiation ���������������������������������������������������������������������������������������� 685 Explicit Template Instantiation ������������������������������������������������������������������������������������������������������������ 686 Testing the Array Class Template ��������������������������������������������������������������������������������� 687 Non-Type Class Template Parameters �������������������������������������������������������������������������� 690 Templates for Member Functions with Non-Type Parameters ����������������������������������������������������������� 691 Arguments for Non-Type Parameters ������������������������������������������������������������������������������������������������� 695 Non-Type Template Arguments vs� Constructor Arguments ��������������������������������������������������������������� 696 Default Values for Template Parameters ���������������������������������������������������������������������� 697 Class Template Argument Deduction ���������������������������������������������������������������������������� 697 Class Template Specialization �������������������������������������������������������������������������������������� 700 Defining a Class Template Specialization ������������������������������������������������������������������������������������������� 700 Partial Template Specialization ����������������������������������������������������������������������������������������������������������� 701 Class Templates with Nested Classes �������������������������������������������������������������������������� 702 Function Templates for Stack Members ��������������������������������������������������������������������������������������������� 704 Dependent Names Nuisances �������������������������������������������������������������������������������������� 709 Dependent Type Names ���������������������������������������������������������������������������������������������������������������������� 710 Dependent Base Classes �������������������������������������������������������������������������������������������������������������������� 712 Summary ���������������������������������������������������������������������������������������������������������������������� 714 ■Chapter 18: Move Semantics  717 Lvalues and Rvalues ����������������������������������������������������������������������������������������������������� 717 Rvalue References ������������������������������������������������������������������������������������������������������������������������������ 719 Moving Objects ������������������������������������������������������������������������������������������������������������� 719 Defining Move Members ��������������������������������������������������������������������������������������������������������������������� 722 Explicitly Moved Objects ���������������������������������������������������������������������������������������������� 726 Move-Only Types �������������������������������������������������������������������������������������������������������������������������������� 727 Extended Use of Moved Objects ��������������������������������������������������������������������������������������������������������� 728 ■ Table of ConTenTs